Commit c3855df1 authored by Masayuki Tanaka's avatar Masayuki Tanaka

Fix parseDate to handle string correctly - #943

parent d35f0f8b
...@@ -946,10 +946,10 @@ ...@@ -946,10 +946,10 @@
var $$ = this, parsedDate; var $$ = this, parsedDate;
if (date instanceof Date) { if (date instanceof Date) {
parsedDate = date; parsedDate = date;
} else if (typeof date === 'string') {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} else if (typeof date === 'number' || !isNaN(date)) { } else if (typeof date === 'number' || !isNaN(date)) {
parsedDate = new Date(+date); parsedDate = new Date(+date);
} else {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} }
if (!parsedDate || isNaN(+parsedDate)) { if (!parsedDate || isNaN(+parsedDate)) {
window.console.error("Failed to parse x '" + date + "' to Date object"); window.console.error("Failed to parse x '" + date + "' to Date object");
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -50,6 +50,8 @@ describe('c3 chart data', function () { ...@@ -50,6 +50,8 @@ describe('c3 chart data', function () {
}); });
describe('timeseries x', function () { describe('timeseries x', function () {
describe('without xFormat', function () {
it('should load timeseries data successfully', function () { it('should load timeseries data successfully', function () {
args = { args = {
data: { data: {
...@@ -86,28 +88,22 @@ describe('c3 chart data', function () { ...@@ -86,28 +88,22 @@ describe('c3 chart data', function () {
}); });
}); });
describe('milliseconds timeseries x', function () { describe('with xFormat', function () {
describe('timeseries x with xFormat', function () {
describe('as date string', function () { it('should load timeseries data successfully', function () {
it('should update args', function () {
args = { args = {
data: { data: {
x : 'date', x : 'date',
xFormat: '%Y-%m-%d %H:%M:%S.%L', xFormat: '%Y%m%d',
columns: [ columns: [
['date', "2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"], ['date', '20130101', '20130102', '20130103'],
['data1', 30, 200], ['data1', 30, 200, 100],
['data2', 130, 300] ['data2', 130, 300, 200]
] ]
}, },
axis: { axis : {
x: { x : {
type: 'timeseries', type : 'timeseries'
tick: {
format: '%Y-%m-%d %H:%M:%S.%L',
multiline: false
}
} }
} }
}; };
...@@ -116,35 +112,34 @@ describe('c3 chart data', function () { ...@@ -116,35 +112,34 @@ describe('c3 chart data', function () {
it('should have correct number of xs', function () { it('should have correct number of xs', function () {
expect(Object.keys(chart.internal.data.xs).length).toBe(2); expect(Object.keys(chart.internal.data.xs).length).toBe(2);
expect(chart.internal.data.xs.data1.length).toBe(2); expect(chart.internal.data.xs.data1.length).toBe(3);
expect(chart.internal.data.xs.data2.length).toBe(2); expect(chart.internal.data.xs.data2.length).toBe(3);
}); });
it('should have Date object as x', function () { it('should have Date object as x', function () {
var xs = chart.internal.data.xs; var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123)); expect(+xs.data1[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
expect(+xs.data1[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345)); expect(+xs.data1[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
expect(+xs.data2[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123)); expect(+xs.data1[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
expect(+xs.data2[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345)); expect(+xs.data2[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
expect(+xs.data2[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
expect(+xs.data2[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
}); });
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]);
}); });
}); });
}); });
describe('as unixtime number', function () { describe('milliseconds timeseries x', function () {
describe('as date string', function () {
it('should update args', function () { it('should update args', function () {
args = { args = {
data: { data: {
x : 'date', x : 'date',
xFormat: '%Y-%m-%d %H:%M:%S.%L',
columns: [ columns: [
['date', 1417622461123, 1417622522345], ['date', "2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"],
['data1', 30, 200], ['data1', 30, 200],
['data2', 130, 300] ['data2', 130, 300]
] ]
...@@ -153,7 +148,8 @@ describe('c3 chart data', function () { ...@@ -153,7 +148,8 @@ describe('c3 chart data', function () {
x: { x: {
type: 'timeseries', type: 'timeseries',
tick: { tick: {
format: '%Y-%m-%d %H:%M:%S.%L' format: '%Y-%m-%d %H:%M:%S.%L',
multiline: false
} }
} }
} }
...@@ -169,21 +165,29 @@ describe('c3 chart data', function () { ...@@ -169,21 +165,29 @@ describe('c3 chart data', function () {
it('should have Date object as x', function () { it('should have Date object as x', function () {
var xs = chart.internal.data.xs; var xs = chart.internal.data.xs;
expect(+xs.data1[0]).toBe(1417622461123); expect(+xs.data1[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
expect(+xs.data1[1]).toBe(1417622522345); expect(+xs.data1[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345));
expect(+xs.data2[0]).toBe(1417622461123); expect(+xs.data2[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
expect(+xs.data2[1]).toBe(1417622522345); 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]);
}); });
}); });
describe('as unixtime string', function () { });
describe('as unixtime number', function () {
it('should upate args', function () { it('should update args', function () {
args = { args = {
data: { data: {
x : 'date', x : 'date',
columns: [ columns: [
['date', "1417622461123", "1417622522345"], ['date', 1417622461123, 1417622522345],
['data1', 30, 200], ['data1', 30, 200],
['data2', 130, 300] ['data2', 130, 300]
] ]
...@@ -192,8 +196,7 @@ describe('c3 chart data', function () { ...@@ -192,8 +196,7 @@ describe('c3 chart data', function () {
x: { x: {
type: 'timeseries', type: 'timeseries',
tick: { tick: {
format: '%Y-%m-%d %H:%M:%S.%L', format: '%Y-%m-%d %H:%M:%S.%L'
multiline: false
} }
} }
} }
...@@ -214,7 +217,6 @@ describe('c3 chart data', function () { ...@@ -214,7 +217,6 @@ describe('c3 chart data', function () {
expect(+xs.data2[0]).toBe(1417622461123); expect(+xs.data2[0]).toBe(1417622461123);
expect(+xs.data2[1]).toBe(1417622522345); expect(+xs.data2[1]).toBe(1417622522345);
}); });
}); });
}); });
......
...@@ -941,10 +941,10 @@ c3_chart_internal_fn.parseDate = function (date) { ...@@ -941,10 +941,10 @@ 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 === 'string') {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} else if (typeof date === 'number' || !isNaN(date)) { } else if (typeof date === 'number' || !isNaN(date)) {
parsedDate = new Date(+date); parsedDate = new Date(+date);
} else {
parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
} }
if (!parsedDate || isNaN(+parsedDate)) { if (!parsedDate || isNaN(+parsedDate)) {
window.console.error("Failed to parse x '" + date + "' to Date object"); window.console.error("Failed to parse x '" + date + "' to Date object");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment