<commit_before>/*
 * == BSD2 LICENSE ==
 * Copyright (c) 2014, Tidepool Project
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the associated License, which is identical to the BSD 2-Clause
 * License as published by the Open Source Initiative at opensource.org.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the License for more details.
 * 
 * You should have received a copy of the License along with this program; if
 * not, you can obtain one from Tidepool Project at tidepool.org.
 * == BSD2 LICENSE ==
 */

var _ = require('lodash');
var d3 = require('d3');

var format = require('../../../data/util/format');

var log = require('bows')('DailyX');

module.exports = function(pool, opts) {

  var defaults = {
    dayShiftY: 2,
    textShiftX: 5,
    textShiftY: 5,
    tickLength: 15
  };

  opts = _.defaults(opts || {}, defaults);

  var mainGroup = pool.parent();

  var stickyLabel = mainGroup.select('#tidelineLabels')
    .append('g')
    .attr('class', 'd3-axis')
    .append('text')
    .attr({
      'class': 'd3-day-label',
      x: opts.leftEdge,
      // this is the same as dailyx.dayYPosition
      // we just don't have a datum to pass here
      y: pool.height() - opts.tickLength * 2.5 + opts.dayShiftY
    });

  opts.emitter.on('zoomstart', function() {
    stickyLabel.attr('opacity', '0.2');
  });

  opts.emitter.on('zoomend', function() {
    stickyLabel.attr('opacity', '1.0');
  });

  opts.emitter.on('navigated', function(a) {
    var d = a[0].start;
    // when we're close to midnight (where close = five hours on either side)
    // remove the sticky label so it doesn't overlap with the midnight-anchored day label
    if ((d.getUTCHours() >= 19) || (d.getUTCHours() <= 4)) {
      stickyLabel.text('');
      return;
    }
    stickyLabel.text(format.xAxisDayText(d.toISOString()));
  });

  function dailyx(selection) {

    opts.xScale = pool.xScale().copy();

    selection.each(function(currentData) {
      var ticks = selection.selectAll('g.d3-axis.' + opts['class'])
        .data(currentData, function(d) {
          return d.id;
        });

      var tickGroups = ticks.enter()
        .append('g')
        .attr({
          'class': 'd3-axis ' + opts['class'],
          'clip-path': 'url(#mainClipPath)'
        });

      tickGroups.append('line')
        .attr({
          x1: dailyx.xPosition,
          x2: dailyx.xPosition,
          y1: pool.height(),
          y2: dailyx.tickLength
        });

      tickGroups.append('text')
        .attr({
          x: dailyx.textXPosition,
          y: pool.height() - opts.textShiftY
        })
        .text(function(d) {
          return format.xAxisTickText(d.normalTime);
        });

      tickGroups.filter(function(d) {
        var dt = new Date(d.normalTime);
        if (dt.getUTCHours() === 0) {
          return d;
        }
      })
        .append('text')
        .attr({
          'class': 'd3-day-label',
          x: dailyx.textXPosition,
          y: dailyx.dayYPosition
        })
        .text(function(d) {
          return format.xAxisDayText(d.normalTime);
        });

      ticks.exit().remove();
    });
  }

  dailyx.xPosition = function(d) {
    return opts.xScale(Date.parse(d.normalTime));
  };

  dailyx.textXPosition = function(d) {
    return dailyx.xPosition(d) + opts.textShiftX;
  };

  dailyx.dayYPosition = function(d) {
    return dailyx.tickLength(d) + opts.dayShiftY;
  };

  dailyx.tickLength = function(d) {
    var dt = new Date(d.normalTime);
    if (dt.getUTCHours() === 0) {
      return pool.height() - opts.tickLength * 2.5;
    }
    else return pool.height() - opts.tickLength;
  };

  dailyx.text = function(d) {
    return format(d.normalTime);
  };

  return dailyx;
};<commit_msg>minor tweak to slightly improve positioning of upper date labels in x-axis<commit_after>/*
 * == BSD2 LICENSE ==
 * Copyright (c) 2014, Tidepool Project
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the associated License, which is identical to the BSD 2-Clause
 * License as published by the Open Source Initiative at opensource.org.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the License for more details.
 * 
 * You should have received a copy of the License along with this program; if
 * not, you can obtain one from Tidepool Project at tidepool.org.
 * == BSD2 LICENSE ==
 */

var _ = require('lodash');
var d3 = require('d3');

var format = require('../../../data/util/format');

var log = require('bows')('DailyX');

module.exports = function(pool, opts) {

  var defaults = {
    textShiftX: 5,
    textShiftY: 5,
    tickLength: 15,
    longTickMultiplier: 2.5
  };

  opts = _.defaults(opts || {}, defaults);

  var mainGroup = pool.parent();

  var stickyLabel = mainGroup.select('#tidelineLabels')
    .append('g')
    .attr('class', 'd3-axis')
    .append('text')
    .attr({
      'class': 'd3-day-label',
      x: opts.leftEdge,
      // this is the same as dailyx.dayYPosition
      // we just don't have a datum to pass here
      y: pool.height() - opts.tickLength * opts.longTickMultiplier
    });

  opts.emitter.on('zoomstart', function() {
    stickyLabel.attr('opacity', '0.2');
  });

  opts.emitter.on('zoomend', function() {
    stickyLabel.attr('opacity', '1.0');
  });

  opts.emitter.on('navigated', function(a) {
    var d = a[0].start;
    // when we're close to midnight (where close = five hours on either side)
    // remove the sticky label so it doesn't overlap with the midnight-anchored day label
    if ((d.getUTCHours() >= 19) || (d.getUTCHours() <= 4)) {
      stickyLabel.text('');
      return;
    }
    stickyLabel.text(format.xAxisDayText(d.toISOString()));
  });

  function dailyx(selection) {

    opts.xScale = pool.xScale().copy();

    selection.each(function(currentData) {
      var ticks = selection.selectAll('g.d3-axis.' + opts['class'])
        .data(currentData, function(d) {
          return d.id;
        });

      var tickGroups = ticks.enter()
        .append('g')
        .attr({
          'class': 'd3-axis ' + opts['class'],
          'clip-path': 'url(#mainClipPath)'
        });

      tickGroups.append('line')
        .attr({
          x1: dailyx.xPosition,
          x2: dailyx.xPosition,
          y1: pool.height(),
          y2: dailyx.tickLength
        });

      tickGroups.append('text')
        .attr({
          x: dailyx.textXPosition,
          y: pool.height() - opts.textShiftY
        })
        .text(function(d) {
          return format.xAxisTickText(d.normalTime);
        });

      tickGroups.filter(function(d) {
        var dt = new Date(d.normalTime);
        if (dt.getUTCHours() === 0) {
          return d;
        }
      })
        .append('text')
        .attr({
          'class': 'd3-day-label',
          x: dailyx.textXPosition,
          y: dailyx.dayYPosition
        })
        .text(function(d) {
          return format.xAxisDayText(d.normalTime);
        });

      ticks.exit().remove();
    });
  }

  dailyx.xPosition = function(d) {
    return opts.xScale(Date.parse(d.normalTime));
  };

  dailyx.textXPosition = function(d) {
    return dailyx.xPosition(d) + opts.textShiftX;
  };

  dailyx.dayYPosition = function(d) {
    return dailyx.tickLength(d);
  };

  dailyx.tickLength = function(d) {
    var dt = new Date(d.normalTime);
    if (dt.getUTCHours() === 0) {
      return pool.height() - opts.tickLength * opts.longTickMultiplier;
    }
    else return pool.height() - opts.tickLength;
  };

  dailyx.text = function(d) {
    return format(d.normalTime);
  };

  return dailyx;
};
{'fraction_non_alphanumeric': 0.14537223340040242, 'fraction_numerical': 0.008928571428571428, 'mean_word_length': 2.454821894005213, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899783', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2490, 'n_words': 639}