def __init__(self, domain_min, domain_max): if domain_min >= domain_max: raise ValueError('Domain minimum must be less than domain maximum. Inverted domains are not currently supported.') self._data_min = domain_min self._data_max = domain_max self._ticker = ScoreTimeTicker(self._data_min, self._data_max)
class Temporal(Scale): """ A scale that linearly maps date/datetime values from a domain to a range. :param domain_min: The minimum date/datetime of the input domain. :param domain_max: The maximum date/datetime of the input domain. """ def __init__(self, domain_min, domain_max): if domain_min >= domain_max: raise ValueError('Domain minimum must be less than domain maximum. Inverted domains are not currently supported.') self._data_min = domain_min self._data_max = domain_max self._ticker = ScoreTimeTicker(self._data_min, self._data_max) def project(self, value, range_min, range_max): """ Project a value in this scale's domain to a target range. """ numerator = value - self._ticker.min denominator = self._ticker.max - self._ticker.min # Python 2 does not support timedelta division if six.PY2: if isinstance(self._ticker.min, datetime): numerator = numerator.total_seconds() denominator = denominator.total_seconds() else: numerator = float(numerator.days) denominator = float(denominator.days) pos = numerator / denominator return ((range_max - range_min) * pos) + range_min def project_interval(self, value, range_min, range_max): """ Project a value in this scale's domain to an interval in the target range. This is used for places :class:`.Bars` and :class:`.Columns`. """ raise NotImplementedError def ticks(self): """ Generate a series of ticks for this scale. """ return self._ticker.ticks def format_tick(self, value, i, count): """ Format ticks for display. This method is used as a default which will be ignored if the user provides a custom tick formatter to the axis. """ return self._ticker.format_tick(value)
def test_hours(self): ticker = ScoreTimeTicker( datetime(2011, 3, 5, 2), datetime(2011, 3, 5, 10) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_hour_count)
def test_days_for_months(self): ticker = ScoreTimeTicker( date(2011, 3, 1), date(2011, 5, 1) ) self.assertIsInstance(ticker.ticks[0], date) self.assertIs(ticker._to_unit, utils.to_day_count)
def test_days_datetime(self): ticker = ScoreTimeTicker( datetime(2011, 3, 5), datetime(2011, 3, 10) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_day_count)
def test_months_for_years(self): ticker = ScoreTimeTicker( date(2011, 1, 1), date(2013, 1, 1) ) self.assertIsInstance(ticker.ticks[0], date) self.assertIs(ticker._to_unit, utils.to_month_count)
def test_months_datetime(self): ticker = ScoreTimeTicker( datetime(2011, 3, 1), datetime(2011, 7, 1) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_month_count)
def test_years_datetime(self): ticker = ScoreTimeTicker( datetime(2011, 1, 1), datetime(2015, 1, 1) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_year_count)
def test_microseconds_for_seconds(self): ticker = ScoreTimeTicker( datetime(2011, 3, 5, 2, 15, 15), datetime(2011, 3, 5, 2, 15, 17) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_microsecond_count)
def test_minutes(self): ticker = ScoreTimeTicker( datetime(2011, 3, 5, 2, 15), datetime(2011, 3, 5, 2, 45) ) self.assertIsInstance(ticker.ticks[0], datetime) self.assertIs(ticker._to_unit, utils.to_minute_count)