I have encountered an issue that I am struggling to resolve. I am in the process of creating a Google Area Chart using a JSON response from a server, specifically with date type columns.
Below is the JSON data obtained from the server (copy/paste), organized by date:
{
"cols": [
{"id":"1", "label":"Dates", "pattern":"", "type":"date"},
{"id":"2", "label":"Rate", "pattern":"", "type":"number"}
],
"rows": [
{
"c": [
{"v":"Date(2016,01,23)"},
{"v":1}
]
},
(additional rows included)
]
}
Despite no apparent errors in the data, the final graph displays some unexpected behavior as certain dates, like Date(2016,1,23) and those in February, are being incorrectly transformed into March.
Strangely enough, when I adjust all the dates by shifting them one month forward, the graph reacts appropriately.
{
"cols": [
(updated list of cols)
],
"rows": [
(updated rows mapping)
]
}
A graph depicting the correct behavior can be found here: https://i.stack.imgur.com/BVqzP.png
Any insights on what might be causing this discrepancy? I have verified the order of the dates and tried different browsers, yet the problem persists even when using LineChart instead of AreaCharts.
EDIT
Upon further investigation, I discovered that the issue lies not with the graph itself but with how the JSON response is being parsed.
Utilizing AngularJS, I noticed that all the Date values are automatically parsed but with incorrect months.
To address this, I took a temporary solution by modifying the parsing method as follows:
$http({
method: 'GET',
url: url_to_server,
transformResponse: [function (data) {
return data; \\ returning unparsed response
}]
}).success(function (data) {
data = JSON.parse(data, dateTimeReviver);
});
The `dateTimeReviver` function used for parsing is defined as:
var dateTimeReviver = function (key, value) {
if (typeof value === 'string') {
if(value.indexOf('Date(') > -1){
return eval(value);
}
}
return value;
}
In order to make this workaround effective, I had to alter the server's response to follow the "new Date(2016,01,23)" format. While this solution works for now, it may not be the most elegant or optimal approach. Any suggestions on improving this would be greatly appreciated.