Browse Source

Accept unixtime string as timeseries date input - #805

pull/813/head
Masayuki Tanaka 10 years ago
parent
commit
878e1daa9e
  1. 4
      c3.js
  2. 2
      c3.min.js
  3. 155
      spec/data-spec.js
  4. 4
      src/core.js

4
c3.js

@ -918,8 +918,8 @@
var $$ = this, parsedDate; var $$ = this, parsedDate;
if (date instanceof Date) { if (date instanceof Date) {
parsedDate = date; parsedDate = date;
} else if (typeof date === 'number') { } else if (typeof date === 'number' || !isNaN(date)) {
parsedDate = new Date(date); parsedDate = new Date(+date);
} else { } else {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date); parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} }

2
c3.min.js vendored

File diff suppressed because one or more lines are too long

155
spec/data-spec.js

@ -91,49 +91,134 @@ describe('c3 chart data', function () {
}); });
describe('milliseconds timeseries x', function () { describe('milliseconds timeseries x', function () {
it('should load timeseries data successfully', function () {
args = { describe('as date string', function () {
data: {
x : 'date', it('should update args', function () {
xFormat: '%Y-%m-%d %H:%M:%S.%L', args = {
columns: [ data: {
['date', "2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"], x : 'date',
['data1', 30, 200], xFormat: '%Y-%m-%d %H:%M:%S.%L',
['data2', 130, 300] columns: [
] ['date', "2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"],
}, ['data1', 30, 200],
axis: { ['data2', 130, 300]
x: { ]
type: 'timeseries', },
tick: { axis: {
format: '%Y-%m-%d %H:%M:%S.%L', x: {
multiline: false type: 'timeseries',
tick: {
format: '%Y-%m-%d %H:%M:%S.%L',
multiline: false
}
} }
} }
} };
}; expect(true).toBeTruthy();
expect(true).toBeTruthy(); });
});
it('should have correct number of xs', function () {
expect(Object.keys(chart.internal.data.xs).length).toBe(2);
expect(chart.internal.data.xs.data1.length).toBe(2);
expect(chart.internal.data.xs.data2.length).toBe(2);
});
it('should have Date object as x', function () {
var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
expect(+xs.data1[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345));
expect(+xs.data2[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
expect(+xs.data2[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345));
});
it('should have milliseconds tick format', function () {
var expected = ["2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"];
chart.internal.main.selectAll('.c3-axis-x g.tick text').each(function (d, i) {
expect(d3.select(this).text()).toBe(expected[i]);
});
});
it('should have correct number of xs', function () {
expect(Object.keys(chart.internal.data.xs).length).toBe(2);
expect(chart.internal.data.xs.data1.length).toBe(2);
expect(chart.internal.data.xs.data2.length).toBe(2);
}); });
it('should have Date object as x', function () { describe('as unixtime number', function () {
var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123)); it('should update args', function () {
expect(+xs.data1[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345)); args = {
expect(+xs.data2[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123)); data: {
expect(+xs.data2[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345)); x : 'date',
columns: [
['date', 1417622461123, 1417622522345],
['data1', 30, 200],
['data2', 130, 300]
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d %H:%M:%S.%L'
}
}
}
};
expect(true).toBeTruthy();
});
it('should have correct number of xs', function () {
expect(Object.keys(chart.internal.data.xs).length).toBe(2);
expect(chart.internal.data.xs.data1.length).toBe(2);
expect(chart.internal.data.xs.data2.length).toBe(2);
});
it('should have Date object as x', function () {
var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(+new Date(2014, 11, 3, 16, 1, 1, 123));
expect(+xs.data1[1]).toBe(+new Date(2014, 11, 3, 16, 2, 2, 345));
expect(+xs.data2[0]).toBe(+new Date(2014, 11, 3, 16, 1, 1, 123));
expect(+xs.data2[1]).toBe(+new Date(2014, 11, 3, 16, 2, 2, 345));
});
}); });
it('should have milliseconds tick format', function () { describe('as unixtime string', function () {
var expected = ["2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"];
chart.internal.main.selectAll('.c3-axis-x g.tick text').each(function (d, i) { it('should upate args', function () {
expect(d3.select(this).text()).toBe(expected[i]); args = {
data: {
x : 'date',
columns: [
['date', "1417622461123", "1417622522345"],
['data1', 30, 200],
['data2', 130, 300]
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d %H:%M:%S.%L',
multiline: false
}
}
}
};
expect(true).toBeTruthy();
});
it('should have correct number of xs', function () {
expect(Object.keys(chart.internal.data.xs).length).toBe(2);
expect(chart.internal.data.xs.data1.length).toBe(2);
expect(chart.internal.data.xs.data2.length).toBe(2);
}); });
it('should have Date object as x', function () {
var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(+new Date(2014, 11, 3, 16, 1, 1, 123));
expect(+xs.data1[1]).toBe(+new Date(2014, 11, 3, 16, 2, 2, 345));
expect(+xs.data2[0]).toBe(+new Date(2014, 11, 3, 16, 1, 1, 123));
expect(+xs.data2[1]).toBe(+new Date(2014, 11, 3, 16, 2, 2, 345));
});
}); });
}); });

4
src/core.js

@ -913,8 +913,8 @@ c3_chart_internal_fn.parseDate = function (date) {
var $$ = this, parsedDate; var $$ = this, parsedDate;
if (date instanceof Date) { if (date instanceof Date) {
parsedDate = date; parsedDate = date;
} else if (typeof date === 'number') { } else if (typeof date === 'number' || !isNaN(date)) {
parsedDate = new Date(date); parsedDate = new Date(+date);
} else { } else {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date); parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} }

Loading…
Cancel
Save