Merge pull request #3254 from ianks/minDisplayFormat

Suport minUnit for time scale
This commit is contained in:
Simon Brunel
2016-09-08 13:34:53 +02:00
committed by GitHub
3 changed files with 29 additions and 1 deletions

View File

@@ -224,6 +224,7 @@ round | String | - | If defined, dates will be rounded to the start of this unit
tooltipFormat | String | '' | The moment js format string to use for the tooltip.
unit | String | - | If defined, will force the unit to be a certain type. See [Time Units](#scales-time-units) section below for details.
unitStepSize | Number | 1 | The number of units between grid lines.
minUnit | String | 'millisecond' | The minimum display format to be used for a time unit
#### Date Formats

View File

@@ -48,6 +48,7 @@ module.exports = function(Chart) {
round: false, // none, or override with week, month, year, etc.
displayFormat: false, // DEPRECATED
isoWeekday: false, // override week start day - see http://momentjs.com/docs/#/get-set/iso-weekday/
minUnit: 'millisecond',
// defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
displayFormats: {
@@ -195,7 +196,7 @@ module.exports = function(Chart) {
var labelCapacity = innerWidth / (tickLabelWidth);
// Start as small as possible
me.tickUnit = 'millisecond';
me.tickUnit = me.options.time.minUnit;
me.scaleSizeInUnits = me.lastTick.diff(me.firstTick, me.tickUnit, true);
me.displayFormat = me.options.time.displayFormats[me.tickUnit];

View File

@@ -84,6 +84,7 @@ describe('Time scale tests', function() {
round: false,
isoWeekday: false,
displayFormat: false,
minUnit: 'millisecond',
displayFormats: {
'millisecond': 'h:mm:ss.SSS a', // 11:20:01.123 AM
'second': 'h:mm:ss a', // 11:20:01 AM
@@ -279,6 +280,31 @@ describe('Time scale tests', function() {
expect(scale.ticks).toEqual(['Jan 1, 8PM', 'Jan 1, 9PM', 'Jan 1, 10PM', 'Jan 1, 11PM', 'Jan 2, 12AM', 'Jan 2, 1AM', 'Jan 2, 2AM', 'Jan 2, 3AM', 'Jan 2, 4AM', 'Jan 2, 5AM', 'Jan 2, 6AM', 'Jan 2, 7AM', 'Jan 2, 8AM', 'Jan 2, 9AM', 'Jan 2, 10AM', 'Jan 2, 11AM', 'Jan 2, 12PM', 'Jan 2, 1PM', 'Jan 2, 2PM', 'Jan 2, 3PM', 'Jan 2, 4PM', 'Jan 2, 5PM', 'Jan 2, 6PM', 'Jan 2, 7PM', 'Jan 2, 8PM', 'Jan 2, 9PM']);
});
it('build ticks honoring the minUnit', function() {
var scaleID = 'myScale';
var mockData = {
labels: ["2015-01-01T20:00:00", "2015-01-02T21:00:00"], // days
};
var mockContext = window.createMockContext();
var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
config.time.minUnit = 'day';
var Constructor = Chart.scaleService.getScaleConstructor('time');
var scale = new Constructor({
ctx: mockContext,
options: config, // use default config for scale
chart: {
data: mockData
},
id: scaleID
});
//scale.buildTicks();
scale.update(400, 50);
expect(scale.ticks).toEqual(['Jan 1, 2015', 'Jan 2, 2015', 'Jan 3, 2015']);
});
it('should build ticks using the config diff', function() {
var scaleID = 'myScale';