简体   繁体   English

D3:根据x轴值在y轴上添加刻度

[英]D3: Adding class to tick on y-axis depending on the x-axis value

I am working on a graph, build with d3 lib. 我正在使用d3 lib构建图。

The graph is so simple, Euros on y-axis and money on x-axis, that way I can show the evolution of a bank account over the time. 图表非常简单,y轴为欧元,x轴为货币,因此我可以显示一段时间内银行帐户的演变情况。

I want to add some style to the month labels, depending on the corresponding amount available on the account for that month, tho draw the x-axis I have something like this: 我想为月份标签添加一些样式,具体取决于该月帐户上可用的相应金额,然后绘制x轴,我就可以看到以下内容:

(d3Data contains all the data required to draw the graph) (d3Data包含绘制图形所需的所有数据)

d3Data.xScale = d3.scale.ordinal().domain(
  d3Data.dateStates.map(function(dateState) {
      return dateState.xValue
  })
);

xAxis = d3.svg.axis().scale(d3Data.xScale).orient("bottom")

d3Data.chart.append("g").call(xAxis)

That effectively creates the axis but now, i want to ad to each tick some classes for styling, like 'zero', 'max' or whatever. 这有效地创建了轴,但现在,我想在每个刻度上添加一些样式类,例如“零”,“最大”或其他。

My first approach was to select all ticks and do something like this: 我的第一种方法是选择所有刻度,然后执行以下操作:

d3Data.chart.selectAll(".xAxisItemClass")
  .attr('class', function(data, index) {
    if (d3Data.months[index].value === 0)
      return 'zero'
  });

But d3Data does not exists inside the anonymous function passed as second parameter to attr() call. 但是d3Data在作为第二个参数传递给attr()调用的匿名函数中不存在。 I am kind of noob on d3 so probably I am missing something because such simple this should be easy 我在d3上有点菜鸟,所以可能我错过了一些东西,因为这么简单这应该很容易

Why I have no access to D3data inside? 为什么我内部无法访问D3data? How can I do this? 我怎样才能做到这一点?

Thanks in advance :) 提前致谢 :)

You can add a class to your ticks based on an array defined outside the anonymous function because that array is visible to the anonymous function: it was declared in an outer scope. 可以基于在匿名函数外部定义的数组向刻度添加类,因为该数组对匿名函数可见:它是在外部作用域中声明的。

This is a basic example: I'm using an array called data and I'm setting the class of two ticks: one which the value is 0 (class "zero", January) and other which the value is 11 (class "eleven", March). 这是一个基本示例:我使用的是名为data的数组,并且设置了两个刻度的类别:一个刻度值为0(“零”类,一月),另一个刻度值为11(“十一”类) ”,三月)。 Inspect the axis and you'll see. 检查轴,您会看到。 To make it easier to see, I set .zero to red and .eleven to green in the CSS. 为了更容易地看到,我设置.zero红色和.eleven在CSS绿色。

 var width = 550, height = 200; var data = [{month: "Jan", value: 0}, {month: "Feb", value: 30}, {month: "Mar", value: 11}, {month: "Apr", value: 60}, {month: "May", value: 20}, {month: "Jun", value: 88} ]; var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height); var xScale = d3.scale.ordinal() .domain(data.map(function(d){ return d.month})) .rangeBands([0, width*0.95]) var xAxis = d3.svg.axis().scale(xScale) .orient("bottom"); svg.append("g") .attr("transform", "translate(10,100)") .attr("class", "x axis") .call(xAxis); var ticks = d3.selectAll(".tick text"); ticks.attr("class", function(d,i){ if(data[i].value == 11){ return "eleven"} else if(data[i].value == 0){ return "zero"} }); 
 .axis path, .axis line { fill: none; stroke: #4e5a64; shape-rendering: crispEdges; } .zero{ fill: red } .eleven{ fill:green } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

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

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