如何设置固定号码。 d3.js中轴上的刻度线?

时间:2016-09-21 21:28:42

标签: d3.js

我试图在条形图中显示数据,其中需要在类别中显示可能的最大值。但是,当使用d3.js的tick()方法时,它会自动将刻度调整为数据集中的最大值。

所需的x轴刻度:

c1 -----------> 5

c2 ------> 3

1 2 3 4 5 6 7 8 9 10

1 个答案:

答案 0 :(得分:5)

可以使用ticks

指定轴刻度线

var data = [{"closePriceDate":"Apr-8-2016","closePrice":93.24},{"closePriceDate":"Apr-9-2016","closePrice":95.35}];

var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 500 - margin.left - margin.right,
    height = 100 - margin.top - margin.bottom;

var formatDate = d3.time.format("%b-%e-%Y");

var x = d3.time.scale()
    .domain(d3.extent(data, function(d) { return formatDate.parse(d.closePriceDate); }))
    .range([0, width]);

var xAxis = d3.svg.axis()
    .scale(x)
	.ticks(4) //specify number of ticks 
    .orient("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 + ")");

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
<script src="https://d3js.org/d3.v3.min.js"></script>