How to show more ticks on d3 line chart when zoomed

情到浓时终转凉″ 提交于 2021-01-28 06:15:51

问题


When I zoom in on my d3 line chart, I can't get daily ticks to display. The lowest tick size that will show up are weekly ticks. Maybe my domain is incorrect? Or I need to use transform.rescaleX() like shown here https://bl.ocks.org/stepheneb/29134b7a51a1ede49f6fc57c5a2a5f38

https://jsfiddle.net/urb1qvch/1/

Javascript

// Prep data
var x = d3.timeDays(new Date(2010, 06, 01), new Date(2020, 10, 30));
var y = Array.from({length: x.length}, Math.random).map(n => Math.floor(n * 10) + 5);
var data = x.map((v, i) => {
  return {
    "x": v,
    "y": y[i]
  }
});

  
// Use the margin convention practice 
var margin = {top: 50, right: 50, bottom: 50, left: 50}
var width = 600 - margin.left - margin.right // Use the window's width 
var height = 400 - margin.top - margin.bottom; // Use the window's height

// zoom function for d3v6
// adapt to v6 from this v5 block https://bl.ocks.org/LemoNode/7ac1d41fe75fe7d2d9cb85e78aad6303
var zoom = d3.zoom()
  .on('zoom', (event) => {
    xScale
      .domain(event.transform.rescaleX(xScale2).domain())
      .range([0, width].map(d => event.transform.applyX(d))); //
      
    svg.select(".line")
        .attr("d", line); // zooms line

      svg.select(".x-axis")
        .call(d3.axisBottom(xScale)
      .tickSizeOuter(0)
      .ticks(20)); // zooms axis
  })
  .scaleExtent([1, 32]); // adjust 32 to less zoom less


// X scale - use min and max of scale
var xScale = d3.scaleUtc()
  .domain([d3.min(x), d3.max(x)]) // input
  .range([0, width]); // output

// constant reference point whilst zooming
var xScale2 = d3.scaleUtc()
  .domain([d3.min(x), d3.max(x)]) // input
  .range([0, width]); // output  
  
// Y scale - add 5 for bit of whitespace at top of graph
var yScale = d3.scaleLinear()
  .domain([0, d3.max(y) + 5]) // input 
  .range([height, 0]); // output 

// d3's line generator
var line = d3.line()
  .x(function(d) { return xScale(d.x); }) // set the x values for the line generator
  .y(function(d) { return yScale(d.y); }) // set the y values for the line generator 

// Add the SVG to the page and employ #2
var svg = d3.select("#my_chart").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .call(zoom) // call the zoom
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// clippath to stop line and x-axis 'spilling over'
svg.append("defs").append("clipPath")
  .attr("id", "clip")
  .append("rect")
  .attr("x", 0)
  .attr("width", width)
  .attr("height", height);
    
// call x-axis and apply the clip from the defs
svg.append("g")
  .attr("class", "x-axis")    
  .attr("clip-path", "url(#clip)") // add the clip path!
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(xScale)); // Create an axis component with d3.axisBottom

// Call the y-axis 
svg.append("g")
  .attr("class", "y-axis")
  .call(d3.axisLeft(yScale)); // Create an axis component with d3.axisLeft
    
// Append the path, bind the data, and call the line generator 
svg.append("path")
  .datum(data) // 10. Binds data to the line 
  .attr("class", "line") // Assign a class for styling 
  .attr("clip-path", "url(#clip)") // add the clip path!
  .attr("d", line); // 11. Calls the line generator

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
</head>
<body>
  <h1>My Chart</h1>
  <div id="my_chart"></div>
</body>
</html>

CSS

.line {
    fill: none;
    stroke: #ffab00;
    stroke-width: 1.5;
}
  
.overlay {
  fill: none;
  pointer-events: all;
}

/* Style the dots by assigning a fill and stroke */
.dot {
    fill: #ffab00;
    stroke: #fff;
}
  
  .focus circle {
  fill: none;
  stroke: steelblue;
}

来源:https://stackoverflow.com/questions/65863587/how-to-show-more-ticks-on-d3-line-chart-when-zoomed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!