D3 v7时间刻度从日期限制的平移和缩放

huangapple go评论97阅读模式
英文:

D3 v7 timeScale paning and zooming from-to date limit

问题

D3 v7时间刻度平移和缩放日期限制

大家好
我有一个问题。
我需要为自定义视频播放器创建一个带有限日期的时间刻度。
例如,从2021年1月1日到现在。
我的代码是:

  1. const domainEnd = new Date()
  2. const xScale = d3.scaleTime()
  3. .domain([new Date(domainEnd.getTime() - (30*60*1000)), domainEnd])
  4. .range([0, width]);
  5. const zoomMin = 1;
  6. const zoomMax = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60)
  7. const zoom = d3.zoom()
  8. .scaleExtent([zoomMin, zoomMax])
  9. .on('zoom', (event) => {
  10. const transformer = event.transform.rescaleX(xScale2)
  11. axis.scale(transformer)
  12. axisG.call(axis)
  13. });
  14. const svg = d3.select('#scale')
  15. .append('svg')
  16. .attr('width', width + margin.left + margin.right)
  17. .attr('height', height + margin.top + margin.bottom)
  18. .call(zoom)
  19. const axis = d3.axisBottom(xScale)
  20. const axisG = svg.append('g')
  21. .attr('class', 'x-axis')
  22. .attr('transform', `translate(0,${height})`)
  23. .call(axis)

问题是:
如何限制在两个日期之间缩放和平移刻度?谢谢

英文:

D3 v7 timeScale pan and zoom from-to date limit

Hello everyone
I have an issue.
I need to have a time scale for a custom video player with limited dates.
For ex. from 2021, 1, 1 to Date.now
My code is:

  1. const domainStart = new Date(2021, 1, 1)
  2. const domainEnd = new Date()
  3. const xScale = d3.scaleTime()
  4. .domain([new Date(domainEnd.getTime() - (30*60*1000)), domainEnd])
  5. .range([0, width]);
  6. const zoomMin = 1;
  7. const zoomMax = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60)
  8. const zoom = d3.zoom()
  9. .scaleExtent([zoomMin, zoomMax])
  10. .on('zoom', (event) => {
  11. const transformer = event.transform.rescaleX(xScale2)
  12. axis.scale(transformer)
  13. axisG.call(axis)
  14. });
  15. const svg = d3.select('#scale')
  16. .append('svg')
  17. .attr('width', width + margin.left + margin.right)
  18. .attr('height', height + margin.top + margin.bottom)
  19. .call(zoom)
  20. const axis = d3.axisBottom(xScale)
  21. const axisG = svg.append('g')
  22. .attr('class', 'x-axis')
  23. .attr('transform', `translate(0,${height})`)
  24. .call(axis)

The question is:
How can I limit scale zooming and panning between two dates? Thx

答案1

得分: 0

好的,以下是代码部分的中文翻译:

  1. // 我处理了这个问题。解决方案如下:
  2. const margin = { top: 0, right: 20, bottom: 60, left: 20 };
  3. const width = 600;
  4. const height = 100;
  5. const domainStart = new Date('2022-01-10T00:00:01');
  6. const domainEnd = new Date();
  7. const xScale: ScaleTime<number, number> = d3
  8. .scaleTime()
  9. .domain([domainStart, domainEnd])
  10. .rangeRound([0, width]);
  11. const zoomMin = 1;
  12. const zoomMax = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60);
  13. const zoom = d3.zoom()
  14. .scaleExtent([zoomMin, zoomMax])
  15. .translateExtent([[0, 0], [width, height]])
  16. .extent([[0, 0], [width, height]])
  17. .on('zoom', ({ transform }) => {
  18. axis.scale(transform.rescaleX(xScale));
  19. axisG.call(axis);
  20. });
  21. const svg = d3.select('#scale')
  22. .append('svg')
  23. .attr('width', width)
  24. .attr('height', height + margin.top + margin.bottom)
  25. .call(zoom);
  26. const axis = d3.axisBottom(xScale);
  27. const axisG = svg.append('g')
  28. .attr('class', 'x-axis')
  29. .attr('clip-path', 'url(#clip)')
  30. .attr('transform', `translate(0,${height})`)
  31. .call(axis);
  32. const k = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60 * 30);
  33. const centerDate = xScale.invert((width) / 2);
  34. zoom.scaleTo(svg, k);
  35. zoom.translateTo(svg, centerDate.getTime(), 0);

请注意,我只翻译了代码部分,没有包括其他内容。如果您需要进一步的帮助,请随时提出。

英文:

Ok I handled this issue. the solution:

  1. const margin = {top: 0, right: 20, bottom: 60, left: 20}
  2. const width = 600;
  3. const height = 100;
  4. const domainStart = new Date(&#39;2022-01-10T00:00:01&#39;)
  5. const domainEnd = new Date()
  6. const xScale: ScaleTime&lt;number, number&gt; = d3
  7. .scaleTime()
  8. .domain([domainStart, domainEnd])
  9. .rangeRound([0, width])
  10. const zoomMin = 1;
  11. const zoomMax = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60);
  12. const zoom = d3.zoom()
  13. .scaleExtent([zoomMin, zoomMax])
  14. .translateExtent([[0, 0], [width, height]])
  15. .extent([[0, 0], [width, height]])
  16. .on(&#39;zoom&#39;, ({transform}) =&gt; {
  17. axis.scale(transform.rescaleX(xScale))
  18. axisG.call(axis)
  19. });
  20. const svg = d3.select(&#39;#scale&#39;)
  21. .append(&#39;svg&#39;)
  22. .attr(&#39;width&#39;, width)
  23. .attr(&#39;height&#39;, height + margin.top + margin.bottom)
  24. .call(zoom)
  25. const axis = d3.axisBottom(xScale)
  26. const axisG = svg.append(&#39;g&#39;)
  27. .attr(&#39;class&#39;, &#39;x-axis&#39;)
  28. .attr(&#39;clip-path&#39;, &#39;url(#clip)&#39;)
  29. .attr(&#39;transform&#39;, `translate(0,${height})`)
  30. .call(axis);
  31. const k = (domainEnd.getTime() - domainStart.getTime()) / (1000 * 60 * 30);
  32. const centerDate = xScale.invert((width) / 2);
  33. zoom.scaleTo(svg, k)
  34. zoom.translateTo(svg, centerDate.getTime(), 0)

huangapple
  • 本文由 发表于 2023年5月31日 23:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定