admin管理员组

文章数量:1333429

I want to add custom tick labels on the x axis,like 1,2,3,4,3,2,1 in this pattern. But the code that I am using doesn't show the decreasing numbers.

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

var x = d3.scale.ordinal()
    .domain([1, 2, 3, 4, 5, 4, 3, 2, 1])
    .rangePoints([0, width]);

var xAxis = d3.svg.axis()
    .scale(x)
    .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")
.call(xAxis);

Any kind of help would be appreciated. Thanks.

I want to add custom tick labels on the x axis,like 1,2,3,4,3,2,1 in this pattern. But the code that I am using doesn't show the decreasing numbers.

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

var x = d3.scale.ordinal()
    .domain([1, 2, 3, 4, 5, 4, 3, 2, 1])
    .rangePoints([0, width]);

var xAxis = d3.svg.axis()
    .scale(x)
    .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")
.call(xAxis);

Any kind of help would be appreciated. Thanks.

Share Improve this question edited Dec 21, 2017 at 7:01 Aravind Cheekkallur 3,2056 gold badges28 silver badges41 bronze badges asked Dec 21, 2017 at 5:17 Steve DosonSteve Doson 7134 gold badges19 silver badges36 bronze badges 1
  • 1 Hope it works for you -> stackoverflow./questions/44872048/… – Utkarsh Dubey Commented Dec 21, 2017 at 5:33
Add a ment  | 

1 Answer 1

Reset to default 6

Little hack it needed to solve this issue.

Cause

  • d3.scale.ordinal(), domain calculation in purely mathematical logic. Each domain associated with a range. ( f(x)=y ).
  • Duplication not allowed because it will make ambiguity

Solution

  • Create linear scale with total item in the axis as domain [0, itemLength]
  • While creating axis use tickFormat to find out the index of the element

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

var xdata = ["1", "2", "3", "4", "5", "4", "3", "2", "1", "0"];

var linear = d3.scale.linear()
	.domain([0, 10])
	.range([0, width]);

var xAxis = d3.svg.axis()
	.scale(linear)
	.orient("bottom");

var axis = d3.svg.axis()
	.scale(linear)
	.orient("top")
	.ticks(10)
	.tickFormat(function (d) {
		return xdata[d];
	});

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")
.call(axis);
.axis path, line {
  fill: none;
  stroke: #000;
  stroke-linecap: round;
}
<script src="https://cdnjs.cloudflare./ajax/libs/d3/3.4.11/d3.min.js"></script>

本文标签: javascriptHow to add custom tick labels in d3jsStack Overflow