【问题标题】:d3 bar chart y axis ticks and grid lines above max valued3 条形图 y 轴刻度和网格线高于最大值
【发布时间】:2017-08-01 21:38:46
【问题描述】:

在我的 d3 条形图中,我应该有 Y 轴动态刻度和整数值的网格线(没有刻度和十进制值的网格线)。最高条的顶部也应该有刻度线和网格线。

在我的示例图表中,我将最大值传递给 y 轴域函数,如果最大值未达到下一个范围,我如何确保多一行并打勾。

Fiddle One 这里第二个柱的总值为 53,所以我期待在 55 处再出现一个刻度线。

Fiddle Two对于同一张图表,如果数据低于10,它会带有小数点的线条,如2.5、3.5等。如何排除这些线条。

这里的刻度是根据值计算的。

// Setup svg using Bostock's margin convention

var margin = {top: 20, right: 160, bottom: 35, left: 30};

var width = 960 - margin.left - margin.right,
    height = 500 - 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)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


/* Data in strings like it would be if imported from a csv */

var data = [
  { year: "2006", redDelicious: "10", mcintosh: "15", oranges: "9", pears: "6" },
  { year: "2007", redDelicious: "12", mcintosh: "18", oranges: "9", pears: "14" },
  { year: "2008", redDelicious: "05", mcintosh: "20", oranges: "8", pears: "2" },
  { year: "2009", redDelicious: "01", mcintosh: "15", oranges: "5", pears: "4" },
  { year: "2010", redDelicious: "02", mcintosh: "10", oranges: "4", pears: "2" },
  { year: "2011", redDelicious: "03", mcintosh: "12", oranges: "6", pears: "3" },
  { year: "2012", redDelicious: "04", mcintosh: "15", oranges: "8", pears: "1" },
  { year: "2013", redDelicious: "06", mcintosh: "11", oranges: "9", pears: "4" },
  { year: "2014", redDelicious: "10", mcintosh: "13", oranges: "9", pears: "5" },
  { year: "2015", redDelicious: "16", mcintosh: "19", oranges: "6", pears: "9" },
  { year: "2016", redDelicious: "19", mcintosh: "17", oranges: "5", pears: "7" },
];

var parse = d3.time.format("%Y").parse;


// Transpose the data into layers
var dataset = d3.layout.stack()(["redDelicious", "mcintosh", "oranges", "pears"].map(function(fruit) {
  return data.map(function(d) {
    return {x: parse(d.year), y: +d[fruit]};
  });
}));


// Set x, y and colors
var x = d3.scale.ordinal()
  .domain(dataset[0].map(function(d) { return d.x; }))
  .rangeRoundBands([10, width-10], 0.02);

var y = d3.scale.linear()
  .domain([0, d3.max(dataset, function(d) {  return d3.max(d, function(d) { return d.y0 + d.y; });  })])
  .range([height, 0]);

var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574"];


// Define and draw axes
var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .tickSize(-width, 0, 0)
  .tickFormat(d3.format("d"));

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom")
  .tickFormat(d3.time.format("%Y"));

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis);

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


// Create groups for each series, rects for each segment 
var groups = svg.selectAll("g.cost")
  .data(dataset)
  .enter().append("g")
  .attr("class", "cost")
  .style("fill", function(d, i) { return colors[i]; });

var rect = groups.selectAll("rect")
  .data(function(d) { return d; })
  .enter()
  .append("rect")
  .attr("x", function(d) { return x(d.x); })
  .attr("y", function(d) { return y(d.y0 + d.y); })
  .attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
  .attr("width", x.rangeBand())
  .on("mouseover", function() { tooltip.style("display", null); })
  .on("mouseout", function() { tooltip.style("display", "none"); })
  .on("mousemove", function(d) {
    var xPosition = d3.mouse(this)[0] - 15;
    var yPosition = d3.mouse(this)[1] - 25;
    tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
    tooltip.select("text").text(d.y);
  });


// Draw legend
var legend = svg.selectAll(".legend")
  .data(colors)
  .enter().append("g")
  .attr("class", "legend")
  .attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });

legend.append("rect")
  .attr("x", width - 18)
  .attr("width", 18)
  .attr("height", 18)
  .style("fill", function(d, i) {return colors.slice().reverse()[i];});

legend.append("text")
  .attr("x", width + 5)
  .attr("y", 9)
  .attr("dy", ".35em")
  .style("text-anchor", "start")
  .text(function(d, i) { 
    switch (i) {
      case 0: return "Anjou pears";
      case 1: return "Naval oranges";
      case 2: return "McIntosh apples";
      case 3: return "Red Delicious apples";
    }
  });


// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
  .attr("class", "tooltip")
  .style("display", "none");

tooltip.append("rect")
  .attr("width", 30)
  .attr("height", 20)
  .attr("fill", "white")
  .style("opacity", 0.5);

tooltip.append("text")
  .attr("x", 15)
  .attr("dy", "1.2em")
  .style("text-anchor", "middle")
  .attr("font-size", "12px")
  .attr("font-weight", "bold");

【问题讨论】:

    标签: javascript html css d3.js charts


    【解决方案1】:

    问题 1

    您可能必须使用tickValues 来传递您自己的自定义刻度。如果您对 d3 的操作方式总体上感到满意,您可以添加一个额外的检查来扩展数组:

    var ticks = y.ticks(),
      lastTick = ticks[ticks.length-1],
      newLastTick = lastTick + (ticks[1] - ticks[0]);
    if (lastTick<y.domain()[1]){
      ticks.push(newLastTick);
    }
    y.domain([y.domain()[0], newLastTick]); //<-- adjust domain for further value
    
    // Define and draw axes
    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickSize(-width, 0, 0)
      .tickFormat( function(d) { return d } )
      .tickValues(ticks);
    

    运行代码:

    // Setup svg using Bostock's margin convention
    
    var margin = {top: 15, right: 160, bottom: 35, left: 30};
    
    var width = 960 - margin.left - margin.right,
        height = 500 - 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)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
    
    /* Data in strings like it would be if imported from a csv */
    
    var data = [
      { year: "2006", redDelicious: "10", mcintosh: "15", oranges: "9", pears: "6" },
      { year: "2007", redDelicious: "12", mcintosh: "18", oranges: "9", pears: "14" },
      { year: "2008", redDelicious: "05", mcintosh: "20", oranges: "8", pears: "2" },
      { year: "2009", redDelicious: "01", mcintosh: "15", oranges: "5", pears: "4" },
      { year: "2010", redDelicious: "02", mcintosh: "10", oranges: "4", pears: "2" },
      { year: "2011", redDelicious: "03", mcintosh: "12", oranges: "6", pears: "3" },
      { year: "2012", redDelicious: "04", mcintosh: "15", oranges: "8", pears: "1" },
      { year: "2013", redDelicious: "06", mcintosh: "11", oranges: "9", pears: "4" },
      { year: "2014", redDelicious: "10", mcintosh: "13", oranges: "9", pears: "5" },
      { year: "2015", redDelicious: "16", mcintosh: "19", oranges: "6", pears: "9" },
      { year: "2016", redDelicious: "19", mcintosh: "17", oranges: "5", pears: "7" },
    ];
    
    var parse = d3.time.format("%Y").parse;
    
    
    // Transpose the data into layers
    var dataset = d3.layout.stack()(["redDelicious", "mcintosh", "oranges", "pears"].map(function(fruit) {
      return data.map(function(d) {
        return {x: parse(d.year), y: +d[fruit]};
      });
    }));
    
    
    // Set x, y and colors
    var x = d3.scale.ordinal()
      .domain(dataset[0].map(function(d) { return d.x; }))
      .rangeRoundBands([10, width-10], 0.02);
    
    var y = d3.scale.linear()
      .domain([0, d3.max(dataset, function(d) {  return d3.max(d, function(d) { return d.y0 + d.y; });  })])
      .range([height, 0]);
    
    var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574"];
    
    var ticks = y.ticks(),
        lastTick = ticks[ticks.length-1],
        newLastTick = lastTick + (ticks[1] - ticks[0]);
    if (lastTick<y.domain()[1]){
      ticks.push(newLastTick);
    }
    y.domain([y.domain()[0], newLastTick]);
    
    // Define and draw axes
    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickSize(-width, 0, 0)
      .tickFormat( function(d) { return d } )
      .tickValues(ticks);
    
    
    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom")
      .tickFormat(d3.time.format("%Y"));
    
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);
    
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
    
    
    // Create groups for each series, rects for each segment 
    var groups = svg.selectAll("g.cost")
      .data(dataset)
      .enter().append("g")
      .attr("class", "cost")
      .style("fill", function(d, i) { return colors[i]; });
    
    var rect = groups.selectAll("rect")
      .data(function(d) { return d; })
      .enter()
      .append("rect")
      .attr("x", function(d) { return x(d.x); })
      .attr("y", function(d) { return y(d.y0 + d.y); })
      .attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
      .attr("width", x.rangeBand())
      .on("mouseover", function() { tooltip.style("display", null); })
      .on("mouseout", function() { tooltip.style("display", "none"); })
      .on("mousemove", function(d) {
        var xPosition = d3.mouse(this)[0] - 15;
        var yPosition = d3.mouse(this)[1] - 25;
        tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
        tooltip.select("text").text(d.y);
      });
    
    
    // Draw legend
    var legend = svg.selectAll(".legend")
      .data(colors)
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
     
    legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", function(d, i) {return colors.slice().reverse()[i];});
     
    legend.append("text")
      .attr("x", width + 5)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "start")
      .text(function(d, i) { 
        switch (i) {
          case 0: return "Anjou pears";
          case 1: return "Naval oranges";
          case 2: return "McIntosh apples";
          case 3: return "Red Delicious apples";
        }
      });
    
    
    // Prep the tooltip bits, initial display is hidden
    var tooltip = svg.append("g")
      .attr("class", "tooltip")
      .style("display", "none");
        
    tooltip.append("rect")
      .attr("width", 30)
      .attr("height", 20)
      .attr("fill", "white")
      .style("opacity", 0.5);
    
    tooltip.append("text")
      .attr("x", 15)
      .attr("dy", "1.2em")
      .style("text-anchor", "middle")
      .attr("font-size", "12px")
      .attr("font-weight", "bold");
    svg {
        font: 10px sans-serif;
        shape-rendering: crispEdges;
      }
    
      .axis path,
      .axis line {
        fill: none;
        stroke: #000;
      }
     
      path.domain {
        stroke: none;
      }
     
      .y .tick line {
        stroke: #ddd;
      }
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"&gt;&lt;/script&gt;

    问题 2

    同样,您可以让d3 提出标记,然后过滤掉您不想要的标记:

    var ticks = y.ticks().filter(function(d){
      return parseInt(d) === d; // is it a integer?
    })
    
    // Define and draw axes
    var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickSize(-width, 0, 0)
    .tickValues(ticks);
    

    运行代码:

    // Setup svg using Bostock's margin convention
    
    var margin = {top: 20, right: 160, bottom: 35, left: 30};
    
    var width = 960 - margin.left - margin.right,
        height = 500 - 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)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
    
    /* Data in strings like it would be if imported from a csv */
    
    var data = [
      { year: "2006", redDelicious: "1", mcintosh: "3" },
      { year: "2007", redDelicious: "2", mcintosh: "1"},
      { year: "2008", redDelicious: "0", mcintosh: "2"}
    ];
    
    var parse = d3.time.format("%Y").parse;
    
    
    // Transpose the data into layers
    var dataset = d3.layout.stack()(["redDelicious", "mcintosh", "oranges", "pears"].map(function(fruit) {
      return data.map(function(d) {
        return {x: parse(d.year), y: +d[fruit]};
      });
    }));
    
    
    // Set x, y and colors
    var x = d3.scale.ordinal()
      .domain(dataset[0].map(function(d) { return d.x; }))
      .rangeRoundBands([10, width-10], 0.02);
    
    var y = d3.scale.linear()
      .domain([0, d3.max(dataset, function(d) {  return d3.max(d, function(d) { return d.y0 + d.y; });  })])
      .range([height, 0]);
    
    var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574"];
    
    var ticks = y.ticks().filter(function(d){
    	return parseInt(d) === d;
    })
    
    // Define and draw axes
    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .tickSize(-width, 0, 0)
      .tickValues(ticks);
      
    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom")
      .tickFormat(d3.time.format("%Y"));
    
    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);
    
    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
    
    
    // Create groups for each series, rects for each segment 
    var groups = svg.selectAll("g.cost")
      .data(dataset)
      .enter().append("g")
      .attr("class", "cost")
      .style("fill", function(d, i) { return colors[i]; });
    
    var rect = groups.selectAll("rect")
      .data(function(d) { return d; })
      .enter()
      .append("rect")
      .attr("x", function(d) { return x(d.x); })
      .attr("y", function(d) { return y(d.y0 + d.y); })
      .attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
      .attr("width", x.rangeBand())
      .on("mouseover", function() { tooltip.style("display", null); })
      .on("mouseout", function() { tooltip.style("display", "none"); })
      .on("mousemove", function(d) {
        var xPosition = d3.mouse(this)[0] - 15;
        var yPosition = d3.mouse(this)[1] - 25;
        tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
        tooltip.select("text").text(d.y);
      });
    
    
    // Draw legend
    var legend = svg.selectAll(".legend")
      .data(colors)
      .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
     
    legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", function(d, i) {return colors.slice().reverse()[i];});
     
    legend.append("text")
      .attr("x", width + 5)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "start")
      .text(function(d, i) { 
        switch (i) {
          case 0: return "Anjou pears";
          case 1: return "Naval oranges";
          case 2: return "McIntosh apples";
          case 3: return "Red Delicious apples";
        }
      });
    
    
    // Prep the tooltip bits, initial display is hidden
    var tooltip = svg.append("g")
      .attr("class", "tooltip")
      .style("display", "none");
        
    tooltip.append("rect")
      .attr("width", 30)
      .attr("height", 20)
      .attr("fill", "white")
      .style("opacity", 0.5);
    
    tooltip.append("text")
      .attr("x", 15)
      .attr("dy", "1.2em")
      .style("text-anchor", "middle")
      .attr("font-size", "12px")
      .attr("font-weight", "bold");
      svg {
        font: 10px sans-serif;
        shape-rendering: crispEdges;
      }
    
      .axis path,
      .axis line {
        fill: none;
        stroke: #000;
      }
     
      path.domain {
        stroke: none;
      }
     
      .y .tick line {
        stroke: #ddd;
      }
      
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 这里我只能看到一个问题,有时在 SVG 边界结束时,额外的顶部 y 轴刻度会在 SVG 容器内切开。有什么解决方法吗?我的容器高度和宽度是使用容器元素访问的,而不是硬编码的。
    • @devo,好问题。您应该在进一步填充刻度后调整y.domain。请参阅上面对问题 1 的更新答案。
    猜你喜欢
    • 1970-01-01
    • 2018-08-03
    • 2011-03-29
    • 2016-08-03
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多