简体   繁体   English

给d3序轴标签与刻度名称不同

[英]Give d3 ordinal axis labels different from scale names

I have an ordinal scale with certain labels for the different values. 我有一个序数scale其中包含不同值的某些标签。 I want to display an axis for that scale, where the axis labels are not the same as the scale labels. 我想显示该刻度的轴,其中轴标签与刻度标签不同。 I have this code: 我有这个代码:

var width = 1000
var height = 600
var margins = {
left: 100, // 40
right: 25,
bottom: 50,
top: 0
}

var y = d3.scale.ordinal().domain(["This", "That", "Other"]).rangePoints([margins.top, height-margins.bottom], 1);

var svg = d3.select("#plot").append("svg").attr("width",width).attr("height",height)

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .tickValues(["This is long", "That is long", "Other is long"])

svg.append("g")
  .attr("class", "axis")
  .attr("transform", "translate(" + margins.left + ",0)")
  .classed("yaxis", true)
  .call(yAxis);

This used to work, but apparently there has been some change in d3 in the last few months that causes it to no longer work. 这曾经起作用,但显然在最近几个月d3发生了一些变化,导致它不再起作用。 ( This other question also says I should be able to use tickValues this way.) The tick labels do not appear. 另一个问题也说我应该能够以这种方式使用tickValues 。)不会出现刻度标签。 I get an error in the JavaScript console saying: 我在JavaScript控制台中收到错误消息:

Unexpected value translate(0,NaN) parsing transform attribute. @ http://d3js.org/d3.v3.js:596

Tracing through the code, it appears that d3 is trying to call the scale on each tick value; 跟踪代码,似乎d3试图在每个tick值上调用scale; that is, it is calling y("This is long") , which results in NaN because "This is long" is not in the scale. 也就是说,它调用y("This is long") ,这导致NaN,因为“This is long”不在比例范围内。 I don't know why it's doing this. 我不知道为什么会这样做。

Here is a fiddle showing the problem. 这是一个显示问题的小提琴 If you comment out the .tickValues line, the axis labels show up correctly. 如果注释掉.tickValues行,则轴标签会正确显示。 With that line uncommented, the labels don't work. 如果该行未注释,则标签不起作用。

What is the way to set tick labels for an ordinal axis in d3? 在d3中为顺序轴设置刻度标签的方法是什么?

After fiddling around a bit more I found the answer. 在摆弄了一点之后,我找到了答案。 It seems that now you must use the tickFormat function to define how an axis displays its ticks. 现在看来你必须使用tickFormat函数来定义轴如何显示其刻度。 The solution is to replace the .tickValues line with this: 解决方案是用这个替换.tickValues行:

.tickFormat(function (d) {
  var mapper = {
    "This": "This is long",
    "That": "That is long",
    "Other": "Other is long"
  }
  return mapper[d]
})

The documentation does not make this clear; 文件没有说清楚; it only explicitly discusses "number formatters". 它只是明确地讨论了“数字格式化程序”。 It does not make the API of a "formatter" clear. 它不会使“格式化程序”的API变得清晰。 It seems that a formatter takes a single value from a scale as input and is supposed to return the display version of that value. 看起来格式化程序从一个标尺中取一个值作为输入,并且应该返回该值的显示版本。 For ordinal scales, though, that value may not be a number but a named value on the scale. 但是,对于序数标度,该值可能不是数字,而是标度上的命名值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM