D3.js可视化 - 如何添加Y轴网格线

时间:2016-12-14 21:24:48

标签: javascript d3.js gridlines

我正在尝试将y网格线添加到我创建的d3.js可视化中。我尝试过其他帖子的方法,比如创建一个函数等。

我找到了一些很棒的例子,例如http://bl.ocks.org/hunzy/11110940https://bl.ocks.org/d3noob/c506ac45617cf9ed39337f99f8511218

我尝试过ticksize,但它没有用(可能是因为我写了标签?)。

我也试过这段代码,但得到了d3.axisLeft不存在的错误。

// gridlines in y axis function
  function make_y_gridlines() {   
  return d3.axisLeft(yscale)
    .ticks(25)
    }
// add the Y gridlines
   main.append('g')     
     .attr("class", "grid")
     .call(make_y_gridlines()
        .tickSize(-width)
        .tickFormat("")
    )

然而,我不能让它适用于我的图表(虽然它可能非常简单)。

这是代码:

<!DOCTYPE html>
    <html>
       <head>
          <title>The d3 test</title>
<style>
  .chart {
   }

.main text {
         font: 10px sans-serif;  
 }

.axis line, .axis path {
   shape-rendering: crispEdges;
   stroke: black;
   fill: none;
 }

.tick line{
    opacity: 0.2;
    }

</style>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js"></script>
  </head>
  <body>
    <div class='content'>

    </div>
    <script>
      var data = [[5,1,2],[4,2,2],[5,2,2],[3,3,1],[4,3,1],[5,3,2],[1,4,1],[5,4,2],[5,5,2],[5,6,2],[5,7,2],
      [2,8,1],[4,8,1],[5,8,2],[1,9,1],[3,9,1],[5,9,1],[1,10,2],[2,10,1],[3,10,1],[5,10,2],[1,11,3],[2,11,1],[3,11,2],[4,11,1],[5,11,1],
      [1,12,3],[5,12,5],[1,13,3],[3,13,2],[5,13,2],[1,14,1],[5,14,2],[4,15,2],[5,15,2],[3,16,1],[4,16,1],[5,16,2],[2,17,1],[5,17,2],
      [5,18,2],[5,18,2],[1,19,1],[2,19,1],[3,19,2],[5,19,1],[5,20,2],[4,21,1],[5,21,1],[1,22,1],[5,22,2],[2,23,2],[4,23,1],[5,23,2],[1,24,3],[5,24,1],
                  ];

var margin = {top: 30, right: 100, bottom: 30, left: 150}
  , width = 960 - margin.left - margin.right
  , height = 900 - margin.top - margin.bottom;

var xscale = d3.scale.linear()
          .domain([0, d3.max(data, function(d) { return d[0]; })])
          .range([ 0, width ]);

var yscale = d3.scale.linear()
        .domain([0, d3.max(data, function(d) { return d[1]; })])
        .range([ height, 0 ]);

var rscale = d3.scale.linear()
 .domain([0, d3.max(data, function(d) { return d[2]; })])
 .range([ 0, 25 ]);

var color = d3.scale.category10();

var chart = d3.select('body')

.append('svg:svg')
   .attr('width', width + margin.right + margin.left)
   .attr('height', height + margin.top + margin.bottom)
   .attr('class', 'chart')

var main = chart.append('g')
   .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
   .attr('width', width)
   .attr('height', height)
   .attr('class', 'main')   

// draw the x axis
var xAxis = d3.svg.axis()
    .scale(xscale)
    .orient('bottom')
    .ticks(6)
    .tickFormat(function (d, i) {
        return ['','everyday','once a week','several times a week','1-3 times a month','less than once a month'][d];
    });


main.append('g')
  .attr('transform', 'translate(0,' + height + ')')
  .attr('class', 'main axis date')
  .call(xAxis);

// draw the y axis
var yAxis = d3.svg.axis()
  .scale(yscale)
  .orient('left')
  .ticks(25)
  .tickPadding(10)
  .tickFormat(function (d, i) {
        return ['','A','B','C','D','E','F','G','H','I'
        ,'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'
        ][d];
    });

main.append('g')
  .attr('transform', 'translate(0,0)')
  .attr('class', 'main axis date')
  .call(yAxis);

var g = main.append("svg:g"); 

g.selectAll("scatter-dots")
  .data(data)
  .enter().append("svg:circle")
      .attr("cx", function (d,i) { return xscale(d[0]); } )
      .attr("cy", function (d) { return yscale(d[1]); } )
      .attr("r", function (d) { return rscale(d[2]); } )
      .style("fill", function(d) { return color(d[2]); });
      // .attr("r", 8);


    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

您的示例使用d3的更新版本(分别为v3和v4)。您正在使用的版本2的文档现在从我看到的内容有点稀疏,但您使用的版本可能不包括您想要的刻度调整功能。

如果您将d3.js版本更新为版本3而不是版本2,则可以将y轴刻度设置为在绘图区域内延伸:

  yAxis.innerTickSize(-width)

使用你的代码我只需使用d3.js v3

即可使用
<script src="http://d3js.org/d3.v3.js"></script>

如果没有一些细微的更改,使用d3 v4会破坏您的代码,因为有一些命名空间更改:d3.scale.linear现在是nw d3.scaleLinear

答案 1 :(得分:1)

您正在使用(出于何种原因)D3版本2.虽然建议使用较新版本,但可以创建一些网格线,获取y轴的位置:

d3.selectAll("g.yAxis g") 
    .append("line") 
    .attr("class", "gridline")
    .attr("x1", 0) 
    .attr("y1", 0)
    .attr("x2", width)
    .attr("y2", 0);

这是一个包含您的代码和D3 v2的演示:

&#13;
&#13;
var data = [[5,1,2],[4,2,2],[5,2,2],[3,3,1],[4,3,1],[5,3,2],[1,4,1],[5,4,2],[5,5,2],[5,6,2],[5,7,2],
      [2,8,1],[4,8,1],[5,8,2],[1,9,1],[3,9,1],[5,9,1],[1,10,2],[2,10,1],[3,10,1],[5,10,2],[1,11,3],[2,11,1],[3,11,2],[4,11,1],[5,11,1],
      [1,12,3],[5,12,5],[1,13,3],[3,13,2],[5,13,2],[1,14,1],[5,14,2],[4,15,2],[5,15,2],[3,16,1],[4,16,1],[5,16,2],[2,17,1],[5,17,2],
      [5,18,2],[5,18,2],[1,19,1],[2,19,1],[3,19,2],[5,19,1],[5,20,2],[4,21,1],[5,21,1],[1,22,1],[5,22,2],[2,23,2],[4,23,1],[5,23,2],[1,24,3],[5,24,1],
                  ];

var margin = {top: 30, right: 100, bottom: 30, left: 150}
  , width = 960 - margin.left - margin.right
  , height = 900 - margin.top - margin.bottom;

var xscale = d3.scale.linear()
          .domain([0, d3.max(data, function(d) { return d[0]; })])
          .range([ 0, width ]);

var yscale = d3.scale.linear()
        .domain([0, d3.max(data, function(d) { return d[1]; })])
        .range([ height, 0 ]);

var rscale = d3.scale.linear()
 .domain([0, d3.max(data, function(d) { return d[2]; })])
 .range([ 0, 25 ]);

var color = d3.scale.category10();

var chart = d3.select('body').append('svg:svg')
   .attr('width', width + margin.right + margin.left)
   .attr('height', height + margin.top + margin.bottom)
   .attr('class', 'chart')

var main = chart.append('g')
   .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
   .attr('width', width)
   .attr('height', height)
   .attr('class', 'main')   

// draw the x axis
var xAxis = d3.svg.axis()
    .scale(xscale)
    .orient('bottom')
    .ticks(6)
    .tickFormat(function (d, i) {
        return ['','everyday','once a week','several times a week','1-3 times a month','less than once a month'][d];
    });


main.append('g')
  .attr('transform', 'translate(0,' + height + ')')
  .attr('class', 'main axis date')
  .call(xAxis);

// draw the y axis
var yAxis = d3.svg.axis()
  .scale(yscale)
  .orient('left')
  .ticks(25)
  .tickPadding(10)
  .tickFormat(function (d, i) {
        return ['','A','B','C','D','E','F','G','H','I'
        ,'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'
        ][d];
    });

main.append('g')
  .attr('transform', 'translate(0,0)')
  .attr('class', 'yAxis main axis date')
  .call(yAxis);
	
	d3.selectAll("g.yAxis g") 
        .append("line") 
            .attr("class", "gridline")
            .attr("x1", 0) 
            .attr("y1", 0)
            .attr("x2", width)
            .attr("y2", 0);

var g = main.append("svg:g"); 

g.selectAll("scatter-dots")
  .data(data)
  .enter().append("svg:circle")
      .attr("cx", function (d,i) { return xscale(d[0]); } )
      .attr("cy", function (d) { return yscale(d[1]); } )
      .attr("r", function (d) { return rscale(d[2]); } )
      .style("fill", function(d) { return color(d[2]); });
      // .attr("r", 8);
&#13;
.axis path,
		.axis line {
			fill: none;
			stroke: black;
			shape-rendering: crispEdges;
		}
		
		.gridline {
			opacity: 0.25;
		}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/2.10.0/d3.v2.js"></script>
&#13;
&#13;
&#13;