简体   繁体   English

如何将d3.js中的时间刻度缩放到分钟?

[英]How to limit the zoom of a time scale in d3.js to the minute?

I have a d3v4 chart using time scale on the x axis. 我有一个使用x轴上的时间刻度的d3v4图表。 Everything is working fine, but when I zoom in, it never stops. 一切都很好,但是当我放大时,它永远不会停止。 Is there a way to make it stop zooming when the minute scale is reached? 有没有办法让它在达到分钟刻度时停止变焦?

d3.scaleTime().range([0, width])
...
var zoom = d3.zoom()
      .scaleExtent([1, Infinity])
      .translateExtent([[0, 0], [width, height]])
      .extent([[0, 0], [width, height]])
      .on("zoom", zoomed);

I am able to stop it from zooming if I change Infinity to some number, but the problem is that the size of the scale is different every time so the scale extent needed to reach the minute scale varies. 如果我将Infinity更改为某个数字,我可以阻止它缩放,但问题是每次刻度的大小都不同,因此达到分钟刻度所需的刻度范围会有所不同。

You just need to set your scaleExtent to the proper values. 您只需将scaleExtent设置为正确的值即可。 If "the size of the scale is different every time", just set scaleExtent using the initial domain values: 如果“每次缩放比例的大小”,只需使用初始域值设置scaleExtent

var domainStart = new Date('1/1/2017'),
    domainEnd = new Date('2/1/2017');

var scale = d3.scaleTime()
  .domain([domainStart, domainEnd])
  .range([0, width])

...

// max zoom is the ratio of the initial domain extent to the minimum
// unit that you want to zoom to (1 minute == 1000*60 milliseconds)
var zoomMax = (domainEnd.getTime() - domainStart.getTime()) / (1000*60),
    zoomMin = 1;  

var zoom = d3.zoom()
  .scaleExtent([zoomMin, zoomMax])
  .translateExtent([[0, 0], [width, height]])
  .extent([[0, 0], [width, height]])
  .on("zoom", zoomed);

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

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