简体   繁体   English

d3卡住直方图到轴

[英]d3 stuck histogram to axis

Hi I've got this Histogram made with the d3 library: jsFiddle 嗨,我已经使用d3库制作了此直方图: jsFiddle
It is zoomable, so you can also pan it. 它是可缩放的,因此您也可以平移它。 But I want the histogram to stuck at least to the xAxis so you can't pan into negative values. 但是我希望直方图至少固定在xAxis上,以便您不能陷入负值。 I've tried it by limiting the domain, what works for the axis, but doesn't effect the bars. 我已经尝试通过限制域来进行尝试,该域对轴有效,但不影响钢筋。

redraw {
...
xScale.domain()[0] = xScale.domain()[0] < 0 ? 0 : xScale.domain()[0];
xScale.domain()[1] = xScale.domain()[1] > data.length ? data.length : xScale.domain()[1];
yScale.domain()[0] = yScale.domain()[0] < 0 ? 0 : yScale.domain()[0];
yScale.domain()[1] = yScale.domain()[1] > d3.max(data) ? d3.max(data) : yScale.domain()[1];
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
vis.selectAll(".bar").attr("transform", "translate(" + zoombie.translate() + ")scale(" + zoombie.scale() + ")");
}

Can anybody help me? 有谁能够帮助我?

I'm not very familiar with d3 but this previous answer should give you enough explanation to do what you are looking for. 我不是很熟悉,D3,但这个以前的答案应该给你足够的解释,你在找什么。

here is the result of all those explanations 是所有这些解释的结果

I hope this helps :) 我希望这有帮助 :)

On short order, here's what we got. 在短期内,这就是我们得到的。 First, all the attributes of your visualization except for width are defined in terms of scales. 首先,可视化的所有属性(宽度除外)都是按比例定义的。 You need to fix this first and foremost, so that you don't have to rely on adding a scale and translation transformation to your bars to generate the desired zoom effect. 您首先需要解决此问题,这样就不必依靠在标尺上添加比例和平移变换来生成所需的缩放效果。 There are a few ways to. 有几种方法。 It's preferable to use an ordinal scale , but again, short order. 最好使用序数标度 ,但又要简短。 We're gonna write a function to do it instead: 我们将编写一个函数来代替它:

function barWidth(d, i) {
  return (xScale(i + 1) - xScale(i)) * (data.length / (data.length + 1))
}

Go into your vis and replace widths that you're computing manually with that function. 进入vis并使用该功能替换您要手动计算的宽度。

Next, and I suspect this isn't exactly what you're looking for but it'll get you most of the way there, change your zoom definition to not include the yScale 接下来,我怀疑这并不是您要找的东西,但是它将为您提供大部分帮助,请更改缩放定义以包括yScale

var zoombie = d3.behavior.zoom().x(xScale).scaleExtent([1, 100]).on("zoom", redraw);

What this does is make zoombie behave like a pan. 这是使zoombie表现得像平底锅一样。 Now for redraw: 现在重新绘制:

function redraw() {
  svg.select(".x.axis").call(xAxis);
  vis.selectAll("rect.bar")
      .attr('width', barWidth)
      .attr('x', function(d, i) { return xScale(i); });
}

And you've got yourself a panning bar chart. 而且您已经有了一个平移条形图。 Now in terms of allowing some vertical latitude for panning, it's possible but making it work is more involved, and plus what are you going to do when you zoom into the top of a bar chart? 现在允许平移一些垂直纬度,这是可能的,但制作方面,它的工作是更多地参与,并加上你有什么打算,当你放大到一个条形图的顶部吗?

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

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