Skip to content

Commit

Permalink
Merge pull request #1240 from vega/zqu/export-format
Browse files Browse the repository at this point in the history
Export timeunit.format() for VgTooltip
  • Loading branch information
light-and-salt committed Apr 1, 2016
2 parents c0bd61f + 23fc2b9 commit 68e0c1a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/compile/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {contains, union} from '../util';
import {FacetModel} from './facet';
import {LayerModel} from './layer';
import {Model} from './model';
import {format as timeFormatExpr} from './time';
import {format as timeFormatExpr} from '../timeunit';
import {UnitModel} from './unit';
import {Spec, isUnitSpec, isFacetSpec, isLayerSpec} from '../spec';

Expand Down
50 changes: 0 additions & 50 deletions src/compile/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,6 @@ import {contains, range} from '../util';
import {COLUMN, ROW, SHAPE, COLOR, Channel} from '../channel';
import {TimeUnit} from '../timeunit';

/** returns the template name used for axis labels for a time unit */
export function format(timeUnit: TimeUnit, abbreviated = false): string {
if (!timeUnit) {
return undefined;
}

let timeString = timeUnit.toString();

let dateComponents = [];

if (timeString.indexOf('year') > -1) {
dateComponents.push(abbreviated ? '%y' : '%Y');
}

if (timeString.indexOf('month') > -1) {
dateComponents.push(abbreviated ? '%b' : '%B');
}

if (timeString.indexOf('day') > -1) {
dateComponents.push(abbreviated ? '%a' : '%A');
} else if (timeString.indexOf('date') > -1) {
dateComponents.push('%d');
}

let timeComponents = [];

if (timeString.indexOf('hours') > -1) {
timeComponents.push('%H');
}
if (timeString.indexOf('minutes') > -1) {
timeComponents.push('%M');
}
if (timeString.indexOf('seconds') > -1) {
timeComponents.push('%S');
}
if (timeString.indexOf('milliseconds') > -1) {
timeComponents.push('%L');
}

let out = [];
if (dateComponents.length > 0) {
out.push(dateComponents.join('-'));
}
if (timeComponents.length > 0) {
out.push(timeComponents.join(':'));
}

return out.length > 0 ? out.join(' ') : undefined;
}

/** returns the smallest nice unit for scale.nice */
export function smallestUnit(timeUnit): string {
if (!timeUnit) {
Expand Down
50 changes: 50 additions & 0 deletions src/timeunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,53 @@ export const TIMEUNITS = [
TimeUnit.MINUTESSECONDS,
TimeUnit.SECONDSMILLISECONDS,
];

/** returns the template name used for axis labels for a time unit */
export function format(timeUnit: TimeUnit, abbreviated = false): string {
if (!timeUnit) {
return undefined;
}

let timeString = timeUnit.toString();

let dateComponents = [];

if (timeString.indexOf('year') > -1) {
dateComponents.push(abbreviated ? '%y' : '%Y');
}

if (timeString.indexOf('month') > -1) {
dateComponents.push(abbreviated ? '%b' : '%B');
}

if (timeString.indexOf('day') > -1) {
dateComponents.push(abbreviated ? '%a' : '%A');
} else if (timeString.indexOf('date') > -1) {
dateComponents.push('%d');
}

let timeComponents = [];

if (timeString.indexOf('hours') > -1) {
timeComponents.push('%H');
}
if (timeString.indexOf('minutes') > -1) {
timeComponents.push('%M');
}
if (timeString.indexOf('seconds') > -1) {
timeComponents.push('%S');
}
if (timeString.indexOf('milliseconds') > -1) {
timeComponents.push('%L');
}

let out = [];
if (dateComponents.length > 0) {
out.push(dateComponents.join('-'));
}
if (timeComponents.length > 0) {
out.push(timeComponents.join(':'));
}

return out.length > 0 ? out.join(' ') : undefined;
}

0 comments on commit 68e0c1a

Please sign in to comment.