本文介绍了D3.js可视化-如何添加Y轴网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将y网格线添加到我创建的d3.js可视化文件中.我尝试了其他帖子(例如创建函数等)中的方法.

I am trying to add a y gridline to a d3.js visualization I created. I have tried approaches from other posts like creating a function etc.

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

I found some great examples like this http://bl.ocks.org/hunzy/11110940 and this https://bl.ocks.org/d3noob/c506ac45617cf9ed39337f99f8511218.

我尝试了ticksize,但是没有用(也许是因为我已经写出标签了?).

I tried ticksize, but it didn't work (maybe because I have written out labels?).

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

I also tried this code, but got the error that d3.axisLeft doesn't exist.

// 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("")
    )

但是我无法使其适用于我的图表(尽管它可能非常简单).

Yet I cannot make it work for my graph (although it's probably super simple).

这是代码:

<!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>

推荐答案

您的示例使用的是d3的最新版本(分别为v3和v4).从我所看到的来看,您正在使用的版本2的文档现在有点稀疏,但是您正在使用的版本可能不包含您想要的刻度大小调整功能.

Your examples use more recent versions so of d3 (v3 and v4 respectively). The documentation for version 2, which you are using, is a bit sparse now from what I can see, but the version you are using likely doesn't include the tick sizing functions you want.

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

If you update your d3.js version to version 3 rather than version 2, you can set your y axis ticks to stretch across plot area:

  yAxis.innerTickSize(-width)

使用您的代码,我只需使用d3.js v3就可以使它正常工作

Using your code I got this to work simply by using d3.js v3

<script src="http://d3js.org/d3.v3.js"></script>

在进行一些较小更改的情况下,使用d3 v4会破坏您的代码,因为名称空间有所更改:例如,d3.scale.linear现在是nw d3.scaleLinear.

Without some minor changes, using d3 v4 will break your code as there are some namespace changes: d3.scale.linear is nw d3.scaleLinear now for example.

这篇关于D3.js可视化-如何添加Y轴网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:41