How to use the d3-time.timeYear.offset function in d3-time

To help you get started, we’ve selected a few d3-time examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cityofasheville / simplicity2 / src / app / development / volume / TimeSlider.js View on Github external
function spanOfYears(numYears) {
  return [
    timeYear.offset(timeDay.floor(new Date()), -1 * numYears).getTime(),
    timeDay.floor(new Date()).getTime(),
  ];
}
github orbiting / styleguide / src / components / Chart / Lines.js View on Github external
const columnWidth = Math.floor(width / columns) - 1
  const innerWidth = columnWidth - paddingLeft - paddingRight

  const xValues = data.map(xAccessor)
  let x
  let xTicks
  let xFormat = d => d
  if (props.xScale === 'time') {
    xFormat = timeFormat(props.timeFormat)
    const xTimes = xValues.map(d => d.getTime())
    const domainTime = [min(xTimes), max(xTimes)]
    const domain = domainTime.map(d => new Date(d))
    let yearInteval = Math.round(timeYear.count(domain[0], domain[1]) / 3)

    let time1 = timeYear.offset(domain[0], yearInteval).getTime()
    let time2 = timeYear.offset(domain[1], -yearInteval).getTime()

    x = scaleTime().domain(domain)
    xTicks = [
      domainTime[0],
      sortBy(xTimes, d => Math.abs(d - time1))[0],
      sortBy(xTimes, d => Math.abs(d - time2))[0],
      domainTime[1]
    ]
      .filter(deduplicate)
      .map(d => new Date(d))
  } else {
    let domain = xValues.filter(deduplicate)
    x = scalePoint().domain(domain)
    if (domain.length > 5) {
      let maxIndex = domain.length - 1
      xTicks = [
github orbiting / styleguide / src / components / Chart / Lines.js View on Github external
const columnWidth = Math.floor(width / columns) - 1
  const innerWidth = columnWidth - paddingLeft - paddingRight

  const xValues = data.map(xAccessor)
  let x
  let xTicks
  let xFormat = d => d
  if (props.xScale === 'time') {
    xFormat = timeFormat(props.timeFormat)
    const xTimes = xValues.map(d => d.getTime())
    const domainTime = [min(xTimes), max(xTimes)]
    const domain = domainTime.map(d => new Date(d))
    let yearInteval = Math.round(timeYear.count(domain[0], domain[1]) / 3)

    let time1 = timeYear.offset(domain[0], yearInteval).getTime()
    let time2 = timeYear.offset(domain[1], -yearInteval).getTime()

    x = scaleTime().domain(domain)
    xTicks = [
      domainTime[0],
      sortBy(xTimes, d => Math.abs(d - time1))[0],
      sortBy(xTimes, d => Math.abs(d - time2))[0],
      domainTime[1]
    ]
      .filter(deduplicate)
      .map(d => new Date(d))
  } else {
    let domain = xValues.filter(deduplicate)
    x = scalePoint().domain(domain)
    if (domain.length > 5) {
      let maxIndex = domain.length - 1
github Vizzuality / TRASE-frontend / scripts / components / nav / years-brush / years-slider.component.js View on Github external
_onBrushEnd() {
    if (!d3_event.sourceEvent) return;
    if (!d3_event.selection) return;

    var d0 = d3_event.selection.map(this.xScale.invert),
      d1 = d0.map(d3_time_timeYear.round);

    if (d1[0] >= d1[1]) {
      d1[0] = d3_time_timeYear.floor(d0[0]);
      d1[1] = d3_time_timeYear.offset(d0[1]);
    }

    const pixelSelection = d1.map(this.xScale);

    this._moveBrushOverlay(pixelSelection);

    this.brush.transition()
      .call(d3_event.target.move, pixelSelection);

    var startYear = new Date (d1[0]).getFullYear();
    var endYear = new Date (d1[1]).getFullYear() - 1;

    this.callback([startYear, endYear]);
  }