简体   繁体   English

D3.js时间刻度刻度线 - 仅限年份和月份 - 自定义时间格式

[英]D3.js time scale tick marks - Years and months only - Custom time format

I'm trying to make a zoomable chart with time scale on the x axis. 我正在尝试在x轴上制作带有时间刻度的可缩放图表。

The default behaviour with an xScale like so: xScale的默认行为如下:

var x = d3.time.scale()
  .domain([getDate(minDate), getDate(maxDate)])
  .range([0, width]);

and an xAxis like so: 和xAx是这样的:

var xAxis = d3.svg.axis()
  .scale(x);

is almost what I'm after. 几乎就是我所追求的。

But I'd like to have a custom Time Format that shows years, and instead of showing the name of the month, shows just a tick line, without the text (and maybe add a class to these ticks), while still retaining the default behaviour of the axis on zoom. 但是我希望有一个显示年份的自定义时间格式,而不是显示月份的名称,只显示一个刻度线,没有文本(并且可能在这些刻度中添加一个类),同时仍然保留默认值轴上的行为缩放。

Any help would be much appreciated. 任何帮助将非常感激。

See JSfiddle here 请在此处查看JSfiddle

Edit: I imagine that Lars' answer here would be a good approach, so long as it could be applied to a zoomable chart. 编辑:我想Lars的答案在这里是一个很好的方法,只要它可以应用于可缩放的图表。 Any fresh thoughts? 任何新鲜的想法?

This may work. 这可能有效。 To shows years, and instead of showing the name of the month, shows just a tick line, without the text: 要显示年份,而不是显示月份的名称,只显示一个刻度线,没有文本:

var timeAxis = d3.svg.axis()
        .scale(timeScale)
        .orient('bottom')
        .ticks(d3.time.years, 1)//should display 1 year intervals
        .tickFormat(d3.time.format('%Y'))//%Y-for year boundaries, such as 2011
        .tickSubdivide(12);//subdivide 12 months in a year

Resources: 资源:

  1. d3.js major minor tick style ( http://bl.ocks.org/vjpgo/4689130 ) d3.js主要的小刻度风格( http://bl.ocks.org/vjpgo/4689130
  2. scale.ticks and scale.tickFormat() ( https://github.com/mbostock/d3/wiki/Time-Intervals#year ) scale.ticks和scale.tickFormat()( https://github.com/mbostock/d3/wiki/Time-Intervals#year

Sample code that shows a tick for every two hours, and formats it to only show hour and AM/PM: 示例代码,每两个小时显示一个勾号,并将其格式化为仅显示小时和上午/下午:

var timeAxis = d3.svg.axis()
        .scale(timeScale)
        .orient('bottom')
        .ticks(d3.time.hours, 2)
        .tickFormat(d3.time.format('%I%p'));

I ended up writing a small function to check the zoom level and set the axis' amount of ticks accordingly. 我最后写了一个小函数来检查缩放级别并相应地设置轴的刻度数。

function calcTickAmount() {
  if (d3.event.scale > 15) {
    return 3
  } else {
    return 10;
  }
}

Then this function would be called within the update function 然后在更新函数中调用此函数

function draw() {
  xAxis.ticks(calcTickAmount());
  axes.call(xAxis);
}

 // Config SVG var width = 500, height = 300, minDate = '1860', maxDate = '1958'; // Draw SVG element var svgSelect = d3.select('div.tl').append('svg'); // Translate SVG G to accomodate margin var translateGroupSelect = svgSelect .attr('width', width) .attr('height', height) .append('g'); // Define d3 xScale var x = d3.time.scale() .domain([new Date(minDate), new Date(maxDate)]) .range([0, width]); // Define main d3 xAxis var xAxis = d3.svg.axis() .scale(x) .ticks(10); // Draw axes var axes = translateGroupSelect.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + 0 + ")") .call(xAxis); // Define zoom var zoom = d3.behavior.zoom() .x(x) .scaleExtent([1, 32]) .center([width / 2, height / 2]) .size([width, height]) .on("zoom", draw); // Apply zoom behavior to SVG element svgSelect.call(zoom); // Repetitive drawing stuff on every zoom event function draw() { xAxis.ticks(calcTickAmount()); axes.call(xAxis); } function calcTickAmount() { if (d3.event.scale > 15) { return 3 } else { return 10; } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script> <div class="tl"> </div> 

Updated Fiddle 更新小提琴

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

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