简体   繁体   English

在 d3js v5 上格式化 x 轴不起作用

[英]Formatting x-axis on d3js v5 doesn't work

I have tried everything from https://observablehq.com/@d3/styled-axes and other tutorials but no chance: formatting the x-axis doesn't work, for instance with .call(d3.axisBottom(x).ticks(10)) .我已经尝试了https://observablehq.com/@d3/styled-axes和其他教程中的所有内容,但没有机会:格式化 x 轴不起作用,例如使用.call(d3.axisBottom(x).ticks(10)) My chart always shows every tick (> 100) and so it's overlapping.我的图表总是显示每个刻度 (> 100),所以它是重叠的。 The x-axis displays dates, like 2020-03-06. x 轴显示日期,例如 2020-03-06。 Here is my code:这是我的代码:

<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 80, left: 80},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom;

var x = d3.scaleBand().rangeRound([0, width]).paddingInner(0.15),
    y = d3.scaleLinear().rangeRound([height, 0]);

var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.json("10.json").then(function (data) {
    x.domain(data.map(function (d) { return d.TIMESTAMP; }));
    y.domain([0, d3.max(data, function (d) { return Number(d.TURNOVER); })]);

        g.append("g")
            .attr("class", "axis axis--x")
            .attr("transform", "translate(0," + height + ")")
            .call(d3.axisBottom(x).ticks(10));

        g.append("g")
            .attr("class", "axis axis--y")
            .call(d3.axisLeft(y).ticks(10));

        g.selectAll(".bar")
          .data(data)
          .enter().append("rect")
            .attr("class", "bar")
            .attr("x", function(d) { return x(d.TIMESTAMP); })
            .attr("y", function(d) { return y(d.TURNOVER); })
            .attr("width", x.bandwidth())
            .attr("height", function(d) { return height - y(d.TURNOVER); });
    })
    .catch((error) => {
            throw error;
    });

</script>

  • You should use date & time scale for your x scale which is x = d3.scaleTime() .您应该为 x 比例使用日期和时间比例,即x = d3.scaleTime()
  • In ticks(10) , You might want to slice it by month, or days.ticks(10) ,您可能希望按月或天对其进行切片。 For example .ticks(d3.timeDay.every(4)) .例如.ticks(d3.timeDay.every(4)) This will slice to every 4 days.这将切片到每 4 天。
  • You also want to change your date format.您还想更改日期格式。 You can use d3.timeFormat("%m/%d") , which will give you "02/06".您可以使用d3.timeFormat("%m/%d") ,它会给您“02/06”。
  • Here is some example.这是一些例子。 Hope this helps.希望这可以帮助。

 // Let create some json dataset var data = []; for (var i = 0; i < 1000; ++i) { let date = parseInt(Math.random() * 30) + 1; let dateStr = (date < 10) ? '0' + date : date data.push({ TIMESTAMP: '2020-02-' + dateStr.toString(), TURNOVER: Math.random() * 100 }); } // Create svg var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 80, left: 80}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().rangeRound([height, 0]); var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // Find min and max date var minDate = d3.min(data.map(function(d) { return new Date(d.TIMESTAMP); })); var maxDate = d3.max(data.map(function(d) { return new Date(d.TIMESTAMP); })); // Set x and y data domains x.domain([minDate, maxDate]); y.domain([0, d3.max(data, function(d) { return d.TURNOVER; })]); // Create x axis var xAxis = d3.axisBottom(x) .ticks(d3.timeDay.every(4)) // Slice time every 4 days .tickFormat(d3.timeFormat("%m/%d")); // Change time format as m/d // Add x axis g.append('g') .attr('class', 'axis axis--x') .attr('transform', 'translate(0, 10)') .call(xAxis);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <svg width="500" height="100"></svg>

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

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