Related
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
Here is my input data.
I am trying to achive distict count of this data using spcific field
result = distictCount("species")
result = [
{
"species": "Iris-flower",
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7
},
{
"species": "floaw",
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8
},
{
"species": "jennifer",
"sepallengthcm": 5.3,
"sepalwidthcm": 2.9
},
{
"species": "kajol",
"sepallengthcm": 5.1,
"sepalwidthcm": 4.5
},
{
"species": "setosa",
"sepallengthcm": 3.2,
"sepalwidthcm": 2.7
}
]
Above result i am expecting
I am trying to achive minimum value by aggregationg selected fields. for two fields ata time
let GroupMin = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = e;
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
if(parseFloat(e[value1]) > first_value){
first_value = parseFloat(e[value1]);
}
if(parseFloat(e[value2]) > second_value){
second_value = parseFloat(e[value2]);
}
}
return r;
}, {}))
return result
}
GroupMin(data, "setosa", "sepallengthcm", "sepalwidthcm)
My code is not working.
Please take a look.
How can we do that
Thanks
You could first group by a key.
After you have your groups, you can reduce the values and map the fields to the minimum values.
const key = 'species';
const fields = [ 'species', 'sepallengthcm', 'sepalwidthcm' ];
const main = () => {
fetchData().then(data =>
console.log(distictCount(data, key, fields)));
};
const distictCount = (data, key, fields) => {
const groups = data.reduce((result, data) => ({
...result,
[data[key]]: [ ...(result[data[key]] || []), data ]
}), {});
const values = Object.values(groups).map(group =>
fields.reduce((res, field) => ({
...res,
[field]: field === key
? group[0][key]
: Math.min(...group.map(obj => obj[field]))
}), {}));
return values.sort((a, b) =>
a[key].localeCompare(b[key], 'en', { sensitivity: 'base' }));
};
const fetchData = () => Promise.resolve([{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
}, {
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
}, {
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
}, {
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
}, {
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
}, {
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
}, {
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
}, {
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}]);
main();
.as-console-wrapper { top: 0; max-height: 100% !important; }
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
Here is my input
export const groupByMultiple = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = e;
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
first_value += parseFloat(e[value1]);
second_value += parseFloat(e[value2])
}
return r;
}, {}))
return result
}
groupByMultiple(data, "species", "sepallengthcm", "sepalwidthcm")
Above function gives sum by aggregating species and summing sepallengthcm and sepalwidthcm.
Same way i am trying to achieve avg and count. But, unfortunately my methods are not working.
Please take a look
Thanks..
Above function gives sum by aggregating species and summing sepallengthcm and sepalwidthcm.
Same way i am trying to achieve avg and count. But, unfortunately my methods are not working.
Please take a look
Thanks..
You can add new key count for each key in the reduce function. And then with the result array you can call .map() and calculate the average for value1 and value2 and add them as key in the object and then return that object,
data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]
const groupByMultiple = (arr, category, value1, value2) => {
let result = Object.values(arr.reduce(function(r, e) {
let key = e[category];
if (!r[key]) r[key] = {...e, count: 1};
else {
let first_value = parseFloat(r[key][value1])
let second_value = parseFloat(r[key][value2])
first_value += parseFloat(e[value1]);
second_value += parseFloat(e[value2])
r[key][value1] = first_value;
r[key][value2] = second_value;
r[key]['count'] += 1;
}
return r;
}, {}));
return result.map(item => {
item[`average_${value1}`] = item[value1]/item.count;
item[`average_${value2}`] = item[value2]/item.count;
return item;
})
}
const ret = groupByMultiple(data, "species", "sepallengthcm", "sepalwidthcm");
console.log(ret);
Below is the JSON response from api in angularjs,
{
"counts":
[
[
{
"xcount": 0.0,
"ycount": 0.0,
"month": 4
},
{
"xcount": 0.0,
"ycount": 0.0,
"month": 5
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 6
},
{
"xcount": 8.85,
"ycount": 0.0,
"month": 6
},
{
"xcount": 10.17,
"ycount": 0.0,
"month": 7
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 7
},
{
"xcount": 12.0,
"ycount": 0.0,
"month": 8
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 8
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 9
},
{
"xcount": 11.0,
"ycount": 0.0,
"month": 9
}
]
]
}
And as per my requirement i want above response as below
{
"counts":
[
[
{
"xcount": 0.0,
"ycount": 0.0,
"month": 4
},
{
"xcount": 0.0,
"ycount": 0.0,
"month": 5
},
{
"xcount": 8.85,
"ycount": 2.85,
"month": 6
},
{
"xcount": 10.17,
"ycount": 2.85,
"month": 7
},
{
"xcount": 12.0,
"ycount": 2.85,
"month": 8
},
{
"xcount": 11.0,
"ycount": 2.85,
"month": 9
}
]
]
}
var arr = {
"counts": [
[{
"xcount": 0.0,
"ycount": 0.0,
"month": 4
},
{
"xcount": 0.0,
"ycount": 0.0,
"month": 5
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 6
},
{
"xcount": 8.85,
"ycount": 0.0,
"month": 6
},
{
"xcount": 10.17,
"ycount": 0.0,
"month": 7
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 7
},
{
"xcount": 12.0,
"ycount": 0.0,
"month": 8
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 8
},
{
"xcount": 0.0,
"ycount": 2.85,
"month": 9
},
{
"xcount": 11.0,
"ycount": 0.0,
"month": 9
}
]
]
}
arr.counts[0] = arr.counts[0].reduce((acc, cv, i, arr) => {
if (!acc[cv.month]) {
acc[cv.month] = cv;
} else {
acc[cv.month].xcount += cv.xcount
acc[cv.month].ycount += cv.ycount
}
if (i === arr.length - 1) {
return Object.keys(acc).map(key => acc[key])
} else {
return acc;
}
}, {});
console.log(arr.counts[0]);
Using the reduce function we create a temporary object using the month number as the key. This allows us to keep track of unique entries. When we find a duplicate month, we add the xcount and ycount together. Once we reach the end of the list we return the temporary object's keys mapped to the their value. Resulting in a list containing only unique months.
As I understood from your question is to remove duplicates months and if it's the case here is my suggestion above.
Update: got your point about combining by xcount, ycount, and added necessary changes.
var arr = {
"counts": [
[
{
"month": 4,
"xcount": 0,
"ycount": 0
},
{
"month": 5,
"xcount": 0,
"ycount": 0
},
{
"month": 6,
"xcount": 0,
"ycount": 2.85
},
{
"month": 6,
"xcount": 8.85,
"ycount": 0
},
{
"month": 7,
"xcount": 10.17,
"ycount": 0
},
{
"month": 7,
"xcount": 0,
"ycount": 2.85
},
{
"month": 8,
"xcount": 12,
"ycount": 0
},
{
"month": 8,
"xcount": 0,
"ycount": 2.85
},
{
"month": 9,
"xcount": 0,
"ycount": 2.85
},
{
"month": 9,
"xcount": 11,
"ycount": 0
}
]
]
};
var tmpMonths = [];
var tmpObjects = {};
var results = [];
for (var i=0;i<arr.counts[0].length;i++) {
var obj = arr.counts[0][i];
var key = "month_" + obj.month;
if (obj.month && tmpMonths.indexOf(obj.month)===-1) {
tmpMonths.push(obj.month);
tmpObjects[key] = obj;
results.push(obj);
} else {
tmpObjects[key] = {
month: obj.month,
xcount: obj.xcount + tmpObjects[key]["xcount"],
ycount: obj.ycount + tmpObjects[key]["ycount"],
};
}
}
arr.counts[0] = Object.values(tmpObjects);
console.log(JSON.stringify(arr));
I've created a custom function to compile in one array data from different sheets in Google Sheets.
Here is how it goes:
function COMPILER(){
var i;
var c;
var display = [];
var resultado = [];
for (i = 0; i < arguments.length; i++) {
display.push(arguments[i]);
};
for (c = 0; c < display.length; c++) {
resultado = resultado.concat(display[c]);
};
return resultado;
};
The code runs just fine, but I've ran into an unexpected issue. Since there are hidden formulas in between the data sources, my final array is compiled with several null values. The final matrix looks a little like this:
resultado [[3, f, 2, 6, 0, r], [2, 2, t, 5, 6, 8], **[, , , , , ]**, **[, , , , , ]**, **[, , , , , ]**, [4, y, y, 7, 8, 0], **[, , , , , ]**...]
I have no clue how to correctly (and selectively) remove those empty values from within the matrix. I would assume I'd have to read and test the whole matrix and remove each selected value, everything within a loop, but I couldn't do on my own.
This code will help you with your issue, it takes every array within the main array and then checks if there are null values to put them away and build a new clean array.
function testArray(){
var arrTest = [[3, 'f', 2, 6, 0, 'r'], [2, 2, 't', 5, 6, 8],[, , , , , ],[, , , , , ], [, , , , , ], [4, 'y', 'y', 7, 8, 0], [, , , , , ]];
// Initiate an empty array to fill it with the non empty data from your Array
var cleanArr = [];
// Check every array inside your main array
arrTest.forEach(function(el){
// If there are null values, this will clean them
var filteredArr = el.filter(function(e){ return e != null;});
// If the array ends up empty, don't push it into your clean array
if(filteredArr.length) cleanArr.push(filteredArr);
});
Logger.log(cleanArr);
}
Docs
Due to the fact you are handling a lot of operations with arrays, I would recommend you to check this:
Best Practices.
I modified your function to get a range.getValues() Object[][] on a sheet that I had removed one of the rows out from the middle of the page. I used the Array.some method to removed the empty row.
Below I have included the code and the Logger.log outputs
function function101(){
var result = [];
Logger.log(JSON.stringify(arguments));
for(var i=0;i<arguments[0].length;i++) {
Logger.log(arguments[0][i]);
if(arguments[0][i].some(function(e){return e;})) {
result.push(arguments[0][i]);
}
}
Logger.log(result);
return result;
}
//I ran this function
function testfunction101() {
var vA=SpreadsheetApp.getActiveSheet().getDataRange().getValues();
function101(vA);
}
/*
[19-12-06 17:24:49:234 MST] {"0":[["HDR1","HDR2","HDR3","HDR4","HDR5","HDR6","HDR7","HDR8","HDR9","HDR10"],[6,9,5,16,7,8,3,6,17,18],[14,19,12,17,10,13,0,2,19,16],[7,3,13,11,15,14,5,17,9,6],[11,10,11,11,4,5,18,9,11,9],[12,6,2,6,5,12,1,2,4,9],[3,16,11,19,16,12,0,19,8,1],[3,0,5,8,8,12,16,6,4,18],[8,0,3,4,11,2,3,7,14,18],[11,15,16,8,7,4,15,16,16,0],[15,5,11,18,8,15,8,2,8,16],[12,8,1,12,3,1,12,11,12,19],[10,11,9,4,12,11,19,17,4,0],["","","","","","","","","",""],[2,4,1,5,14,3,14,15,13,11],[3,7,0,0,18,15,16,11,8,7],[19,7,7,17,17,9,15,14,15,5],[13,2,19,19,8,15,16,13,16,2],[16,3,16,13,13,15,9,1,17,15],[9,1,4,8,3,1,9,8,19,5],[7,13,17,9,6,2,18,1,3,16]]}
[19-12-06 17:24:49:235 MST] [HDR1, HDR2, HDR3, HDR4, HDR5, HDR6, HDR7, HDR8, HDR9, HDR10]
[19-12-06 17:24:49:236 MST] [6.0, 9.0, 5.0, 16.0, 7.0, 8.0, 3.0, 6.0, 17.0, 18.0]
[19-12-06 17:24:49:237 MST] [14.0, 19.0, 12.0, 17.0, 10.0, 13.0, 0.0, 2.0, 19.0, 16.0]
[19-12-06 17:24:49:238 MST] [7.0, 3.0, 13.0, 11.0, 15.0, 14.0, 5.0, 17.0, 9.0, 6.0]
[19-12-06 17:24:49:239 MST] [11.0, 10.0, 11.0, 11.0, 4.0, 5.0, 18.0, 9.0, 11.0, 9.0]
[19-12-06 17:24:49:239 MST] [12.0, 6.0, 2.0, 6.0, 5.0, 12.0, 1.0, 2.0, 4.0, 9.0]
[19-12-06 17:24:49:240 MST] [3.0, 16.0, 11.0, 19.0, 16.0, 12.0, 0.0, 19.0, 8.0, 1.0]
[19-12-06 17:24:49:241 MST] [3.0, 0.0, 5.0, 8.0, 8.0, 12.0, 16.0, 6.0, 4.0, 18.0]
[19-12-06 17:24:49:242 MST] [8.0, 0.0, 3.0, 4.0, 11.0, 2.0, 3.0, 7.0, 14.0, 18.0]
[19-12-06 17:24:49:242 MST] [11.0, 15.0, 16.0, 8.0, 7.0, 4.0, 15.0, 16.0, 16.0, 0.0]
[19-12-06 17:24:49:243 MST] [15.0, 5.0, 11.0, 18.0, 8.0, 15.0, 8.0, 2.0, 8.0, 16.0]
[19-12-06 17:24:49:243 MST] [12.0, 8.0, 1.0, 12.0, 3.0, 1.0, 12.0, 11.0, 12.0, 19.0]
[19-12-06 17:24:49:244 MST] [10.0, 11.0, 9.0, 4.0, 12.0, 11.0, 19.0, 17.0, 4.0, 0.0]
[19-12-06 17:24:49:244 MST] [, , , , , , , , , ]
[19-12-06 17:24:49:245 MST] [2.0, 4.0, 1.0, 5.0, 14.0, 3.0, 14.0, 15.0, 13.0, 11.0]
[19-12-06 17:24:49:246 MST] [3.0, 7.0, 0.0, 0.0, 18.0, 15.0, 16.0, 11.0, 8.0, 7.0]
[19-12-06 17:24:49:246 MST] [19.0, 7.0, 7.0, 17.0, 17.0, 9.0, 15.0, 14.0, 15.0, 5.0]
[19-12-06 17:24:49:247 MST] [13.0, 2.0, 19.0, 19.0, 8.0, 15.0, 16.0, 13.0, 16.0, 2.0]
[19-12-06 17:24:49:248 MST] [16.0, 3.0, 16.0, 13.0, 13.0, 15.0, 9.0, 1.0, 17.0, 15.0]
[19-12-06 17:24:49:249 MST] [9.0, 1.0, 4.0, 8.0, 3.0, 1.0, 9.0, 8.0, 19.0, 5.0]
[19-12-06 17:24:49:249 MST] [7.0, 13.0, 17.0, 9.0, 6.0, 2.0, 18.0, 1.0, 3.0, 16.0]
[19-12-06 17:24:49:250 MST] [[HDR1, HDR2, HDR3, HDR4, HDR5, HDR6, HDR7, HDR8, HDR9, HDR10], [6.0, 9.0, 5.0, 16.0, 7.0, 8.0, 3.0, 6.0, 17.0, 18.0], [14.0, 19.0, 12.0, 17.0, 10.0, 13.0, 0.0, 2.0, 19.0, 16.0], [7.0, 3.0, 13.0, 11.0, 15.0, 14.0, 5.0, 17.0, 9.0, 6.0], [11.0, 10.0, 11.0, 11.0, 4.0, 5.0, 18.0, 9.0, 11.0, 9.0], [12.0, 6.0, 2.0, 6.0, 5.0, 12.0, 1.0, 2.0, 4.0, 9.0], [3.0, 16.0, 11.0, 19.0, 16.0, 12.0, 0.0, 19.0, 8.0, 1.0], [3.0, 0.0, 5.0, 8.0, 8.0, 12.0, 16.0, 6.0, 4.0, 18.0], [8.0, 0.0, 3.0, 4.0, 11.0, 2.0, 3.0, 7.0, 14.0, 18.0], [11.0, 15.0, 16.0, 8.0, 7.0, 4.0, 15.0, 16.0, 16.0, 0.0], [15.0, 5.0, 11.0, 18.0, 8.0, 15.0, 8.0, 2.0, 8.0, 16.0], [12.0, 8.0, 1.0, 12.0, 3.0, 1.0, 12.0, 11.0, 12.0, 19.0], [10.0, 11.0, 9.0, 4.0, 12.0, 11.0, 19.0, 17.0, 4.0, 0.0], [2.0, 4.0, 1.0, 5.0, 14.0, 3.0, 14.0, 15.0, 13.0, 11.0], [3.0, 7.0, 0.0, 0.0, 18.0, 15.0, 16.0, 11.0, 8.0, 7.0], [19.0, 7.0, 7.0, 17.0, 17.0, 9.0, 15.0, 14.0, 15.0, 5.0], [13.0, 2.0, 19.0, 19.0, 8.0, 15.0, 16.0, 13.0, 16.0, 2.0], [16.0, 3.0, 16.0, 13.0, 13.0, 15.0, 9.0, 1.0, 17.0, 15.0], [9.0, 1.0, 4.0, 8.0, 3.0, 1.0, 9.0, 8.0, 19.0, 5.0], [7.0, 13.0, 17.0, 9.0, 6.0, 2.0, 18.0, 1.0, 3.0, 16.0]]
I'm not sure if this was your problem but you said I've created a custom function to compile in one array data from different sheets in Google Sheets. and my input data was gathered from a sheet.getDataRange() method.
How do you prevent Highcharts from showing a cluttered x-axis labels when using dates in Highcharts?
I'm generating my graph with:
<script type="text/javascript">
$(function () {
$.getJSON("get_data", function(data) {
data.plotOptions = {
line: {
marker: {
enabled: false
}
}
};
data.chart.zoomType = 'x';
data.legend = {
layout: 'vertical',
align: 'center',
verticalAlign: 'top',
x: 10,
y: 100,
borderWidth: 0
};
data.xAxis.type = 'datetime';
data.xAxis.title = {text: 'Date'};
$('#container').highcharts(data);
});
});
</script>
and the JSON returned by get_data path looks like:
{"yAxis": {}, "title": {"style": {}, "text": null}, "series": [{"name": "Apple", "data": [1.0, -3.0, 1.0, 4.0, -5.0, 4.0, -1.0, 4.0, 2.0, -1.7741935483871, 3.0, -5.0, 3.0, 2.0, 0.0, -3.0, 1.0, -2.0, 2.0, 4.0, 1.0, -5.0, 5.0, -4.0, 0.0, 4.0, 2.0, -2.0, 0.0, 1.0, -2.0, 4.0, 0.0, -5.0, -5.0, 0.0, -5.0, 3.0, -1.0, 1.0, 4.0, 2.0, -5.0, 4.0, 0.0, 2.0, 1.0, -3.0, 5.0, -1.0, 5.0, -2.0, -4.0, 4.0, -0.324324324324324, -1.0, 3.0, -1.0, 3.0, -3.0, 5.0, 4.0, -3.0, 0.0, 4.0, 0.0, -1.678456591639871, 4.324675324675325, 4.436681222707424, 5.291545189504373, -4.0, -1.0, 3.272479564032698, 3.0, -0.738219895287958, -5.0, -3.0, 4.0, 2.0, -0.719101123595506, -5.0, 5.0, -3.202127659574468, 4.0, 1.3597122302158269, -3.74055415617128, 1.32450331125828, 1.33003300330033, -2.358974358974359, 4.0, 3.0, 4.25391849529781, 4.02564102564103, -0.49622166246851396, 0.86335403726708, 0.628930817610063, -2.89227166276347, 0.08641975308642014, 6.09597523219814, 4.5294117647058805, 1.7272727272727302, 4.406326034063261, -1.2431077694235602, 2.49019607843137, 3.2, 6.06060606060606, 4.75, 7.66830466830467, 8.0952380952381, 12.07692307692308, 3.10628019323671, 8.24242424242424, 2.5853658536585398, 8.006006006006011, -1.9969969969969998, 7.571428571428569, 3.70570570570571, 3.37017994858612, 7.75229357798165, 9.01567398119122, 6.10772833723653, 4.84615384615385, 0.7151702786377698, -1.25, 4.73186119873817, 6.68098159509202, 3.50877192982456, 5.147699757869249, 6.68098159509202, 8.64077669902913, 3.71517027863777, -1.3414634146341502, 5.53012048192771, 9.08805031446541, 8.146341463414629, 2.7021943573667704, 7.77777777777778, 7.703703703703701, 5.97628458498024, 3.3824701195219102, 4.6, 10.11620795107034, 5.60663507109005, 5.21674876847291, 8.96341463414634, -2.5, 6.05882352941176, -1.2392638036809802, 1.8679706601466997, 11.306306306306311, 1.0752351097178696, 0.8929440389294401, 9.4380664652568, 5.10059171597633, 11.33333333333333, 9.882352941176471, 5.10059171597633, 1.9069069069069098, 6.69607843137255, 5.56716417910448, 3.05952380952381, 9.918032786885249, 6.51807228915663, 5.43137254901961, 6.42477876106195, 9.68384074941452, 2.98507462686567, 7.43786982248521, 7.25581395348837, 0.9215686274509798, 9.65116279069767, 2.1515151515151496, 7.71698113207547, 2.82352941176471, -1.1896955503512898, -0.48093841642229007, 4.71976401179941, 6.94117647058824, 10.56844547563805, 3.95626822157434, 6.6379821958457, 2.31400966183575, 7.33734939759036, 8.48837209302326, 6.62060889929742, 5.76368876080692, 10.341246290801191, 5.43735224586288, 3.5527065527065496, 1.4631828978622297, 2.25075528700906, 8.7463556851312, 8.84955752212389, 3.5268065268065296, 6.720823798627, 4.94296577946768, 3.08083140877598, 8.88235294117647, 2.89473684210526, 2.55555555555556, 8.7246376811594, 1.9767441860465098, 15.7558139534884, 10.64705882352941, 8.3053435114504, 5.48854961832061, 8.142857142857139, 2.85480093676815, 10.387205387205391, 9.0, 3.94117647058824], "showInLegend": true}, {"name": "Orange", "data": [2.0, 2.0, 4.0, -4.0, 3.0, -1.0, 0.0, 2.0, 4.0, 3.0, -4.0, 2.0, -2.0, 1.0, 2.0, 3.0, 0.0, -1.0, 0.0, -2.0, 4.0, 1.0, 5.0, 4.0, -1.0, -3.0, -5.0, -3.0, -3.0, 3.0, -2.0, -2.0, 0.0, 3.0, -3.0, -1.0, 0.0, 5.0, 4.0, -3.0, -2.0, 0.0, 0.0, 4.0, 0.0, 0.0, -4.0, 0.0, -3.0, 2.0, -1.0, 4.0, -2.0, -3.0, -5.0, 1.0, -5.0, -1.0, -1.0, 1.0, 1.0, -3.0, 5.0, -4.0, -3.0, -3.0, -4.0, -4.0, -5.0, 1.0, 5.0, 1.0, -1.0, 0.0, 2.0, -2.5555555555555562, -1.0, 0.0, -1.0, 5.0, 5.467289719626168, 0.0, -2.0, 1.0, 1.0, 0.0, -1.0, -2.0, 4.0, 5.0, 3.0, 3.0, -3.0, 5.0, -3.0, 0.0, 1.0, -4.0, -5.0, 1.0, -3.0, 3.0, 5.0, 3.0, 5.0, -5.0, 5.0, 5.0, 0.0, -2.0, -3.0, 4.0, 3.0, -5.0, -5.0, 1.0, 3.0, -4.74293059125964, -5.0, 0.313479623824451, 5.0, 3.0, 4.0, 1.0, -2.0, -1.0, 2.0, -3.757869249394673, 4.0, 5.0, 0.0, 0.48780487804878, 1.0, -4.0, 2.0, 5.0, 4.3086419753086425, 0.0, -5.0, 1.0, 4.0, -3.0, -4.0, 3.0, 1.0, -5.0, 1.0, -1.0, 3.0, -1.0, 3.0, 1.0, 1.0, 4.0, -3.702380952380952, 1.0, 5.0, 0.0, 5.0, -1.0, 3.0, 1.0, -3.0, 1.0, -2.0, 2.234192037470726, -5.0, -2.0, -3.0, -2.0, 2.0, 2.0, 4.0, 1.294117647058824, -5.0, -2.0, 0.0, 5.0, -3.0, 1.0, 4.0, -2.758454106280193, 5.0, 4.0, 4.17096018735363, -3.84726224783862, -3.0, -2.0543735224586293, 4.854700854700855, -1.33729216152019, 0.906344410876133, 3.8746355685131197, 1.17994100294985, 7.29370629370629, 1.03432494279176, 0.8022813688212902, 8.00461893764434, 9.705882352941181, 4.39097744360902, 2.00925925925926, 4.869565217391304, 3.232558139534884, 2.8720930232558137, -2.117647058823529, 2.954198473282443, -2.236641221374046, 1.0, 4.702576112412178, 3.0, 5.680851063829787, -3.0], "showInLegend": true}, {"name": "Pear", "data": [4.0, 1.0, -2.0, 1.0, 1.0, -4.0, -1.0, -5.0, -1.0, 0.0, -2.0, 1.0, 1.0, -2.0, -4.0, -4.0, 3.0, 2.0, -5.0, 1.0, 1.0, -1.0, -4.0, 5.0, -5.0, 0.0, 5.0, 4.0, -2.0, 5.0, -1.0, -2.0, 4.0, -1.0, -5.0, 4.0, -3.0, 4.0, 5.0, 5.0, -1.0, 0.0, 3.0, 3.50375939849624, -2.0, 5.0, -5.0, -5.0, -3.0, 1.0, -4.0, 3.0, -2.0, 2.980392156862745, 5.0, 3.0, 0.0, -4.0, 0.0, -3.0, -2.0, -1.0, 4.0, 5.0, 3.24330900243309, 3.0, 3.643086816720257, -4.675324675324675, 3.31004366812227, -3.7084548104956268, 4.0, 0.51150895140665, 5.544959128065395, -4.553571428571429, 0.0, 5.0, 1.0, 4.248756218905473, 0.0, 4.561797752808989, 3.467289719626168, -1.74025974025974, 3.265957446808511, -2.326599326599327, 3.359712230215827, 2.0, 0.0, 1.33003300330033, -5.0, -1.757281553398058, -4.696048632218845, 4.0, 4.256410256410256, 5.251889168765743, 2.0, 0.628930817610063, -2.0, -0.691358024691358, 0.0, 0.294117647058824, 5.303030303030303, 0.24330900243309, 3.0, 3.0, -1.4, -5.0, -1.583333333333333, -2.017199017199017, 3.297619047619048, 5.0, 3.241545893719807, -5.0, -3.0, -0.6996996996997, -4.0, 4.255102040816327, -2.6996996996997, 2.7712082262210798, -5.0, 2.0, 2.0, -4.408284023668639, -0.690402476780186, -2.75, 3.315457413249211, -0.386503067484663, -3.707602339181287, 2.0, 2.920245398773006, -4.0, -4.0, -0.51219512195122, -1.698795180722892, 3.0, 1.24390243902439, 0.5673981191222599, 2.617283950617284, 0.493827160493827, 2.395256916996047, 2.398406374501992, -3.0, -4.388379204892966, -5.0, 2.246305418719212, 1.6097560975609762, -2.0, 1.0, 2.0, -4.755501222493888, -1.7987987987988, -2.0595611285266457, -0.75669099756691, 1.302114803625378, 2.4792899408283997, -5.0, 0.235294117647059, -2.0, -1.0, 1.0, 0.597014925373134, -3.404761904761905, 1.7025761124121779, -2.0, -3.264705882352941, 3.294985250737463, 5.234192037470726, 2.5970149253731343, 5.29585798816568, -1.767441860465116, 5.0, 5.465116279069767, 0.303030303030303, 4.471698113207547, 4.882352941176471, -3.297423887587822, 0.586510263929619, -1.705014749262537, 0.0, -3.535962877030162, -2.7084548104956268, 4.29673590504451, 2.241545893719807, -4.759036144578313, -4.0, -1.0, 0.5936599423631099, 2.18694362017804, 1.0189125295508301, 4.12820512820513, 6.37529691211401, 2.02114803625378, 3.08163265306122, 7.71976401179941, 8.4219114219114, 17.6453089244851, 3.22433460076046, 17.2401847575058, 15.8235294117647, 16.3007518796992, 19.75, 14.681159420289902, 22.2325581395349, 40.5697674418605, 40.3529411764706, 51.618320610687, 47.6564885496183, 33.7142857142857, 39.0023419203747, 40.026936026936, 26.2127659574468, 24.9019607843137], "showInLegend": true}, {"name": "Banana", "data": [0.0, 2.0, -5.0, 4.0, 0.0, 1.0, -1.0, 5.0, 4.0, 8.2258064516129, 2.0, -5.0, 2.0, 4.0, 3.0, -3.0, -5.0, 7.526315789473699, 5.0, 2.0, 3.0, 3.0, -3.0, 4.0, -1.0, 6.61290322580645, -3.0, 3.0, 5.0, 0.0, -5.0, -3.0, 0.0, 0.0, -5.0, -1.0, -3.0, 3.0, -3.0, 2.0, 4.0, -2.9010989010988997, 3.0, -2.0, 2.0, 3.0, 0.934579439252336, 2.0, -2.0, -5.0, 0.0, 5.0, -1.0, -3.0, -5.0, -1.159663865546218, 4.0, 5.0, -1.3103448275862069, -2.0, -0.25373134328358204, -4.0, 5.0, 4.0, 2.0, 1.0, -0.678456591639871, 5.0, 4.0, -2.7084548104956268, -4.0, 0.255754475703325, 2.272479564032698, -2.553571428571429, 5.261780104712042, -3.0, -4.0, -0.7512437810945269, -0.367088607594937, 2.561797752808989, 5.0, -4.0, -2.0, -2.663299663299663, -0.640287769784173, 2.755667506297229, 1.993377483443709, -1.33993399339934, -1.358974358974359, -2.514563106796116, 1.60790273556231, 5.0, -3.230769230769231, 2.503778337531486, 0.86335403726708, 4.943396226415094, 5.512880562060889, 5.703703703703701, 1.4767801857585101, 5.6470588235294095, -1.5757575757575801, 1.4330900243309, 1.00250626566416, 1.96078431372549, -4.2, 1.51515151515152, 4.25, 3.47420147420147, 2.8928571428571432, 2.0, 0.483091787439614, 4.303030303030303, -2.0, 3.0, 0.3003003003003, 0.53061224489796, -4.099099099099099, 2.51413881748072, -3.694189602446483, 0.940438871473354, -0.06323185011709598, -1.5207100591716, 1.619195046439629, 5.0, 0.946372239747634, -2.386503067484663, 3.0, 5.0, 0.613496932515337, -4.271844660194175, 4.928792569659443, 1.24390243902439, -3.698795180722892, -0.371069182389937, -1.04878048780488, -0.373040752351097, -0.07407407407407396, 1.4938271604938271, -2.604743083003953, -2.800796812749004, -3.2, -3.7767584097859297, -3.052132701421801, 0.492610837438424, 5.304878048780488, 1.3125, 1.470588235294118, 0.920245398773006, 3.488997555012225, -4.399399399399399, 3.626959247648903, -2.75669099756691, 3.7190332326284, -4.0, 4.892857142857143, -0.8235294117647101, 3.4792899408283997, -2.099099099099099, 3.0, -3.0, -2.0, -1.0, -0.39759036144578297, 2.0, 3.294985250737463, -4.297423887587822, 0.597014925373134, 4.887573964497041, 4.465116279069767, -2.607843137254902, 4.930232558139535, 3.606060606060606, 2.0, -4.117647058823529, -2.0, 1.0, 3.589970501474926, 5.0, -2.0, 3.0, 3.0, -0.5507246376811601, -2.34939759036145, 0.6511627906976702, -2.42388758782201, -2.84726224783862, 3.18694362017804, -0.9267139479905402, 7.279202279202281, 8.03800475059382, -2.88519637462236, -0.9591836734693899, 2.65486725663717, 11.09090909090909, 2.32265446224256, 0.7034220532319404, 6.2378752886836, 7.823529411764699, 18.5338345864662, 13.3518518518519, 18.6231884057971, 23.6046511627907, 20.3139534883721, 12.23529411764706, 11.10687022900763, 5.81679389312977, 2.71428571428571, -0.36065573770492, 1.1986531986531999, 1.70212765957447, 3.96078431372549], "showInLegend": true}], "chart": {}, "colorAxis": {}, "xAxis": {"categories": ["2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01", "2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01", "2000-09-01", "2000-10-01", "2000-11-01", "2000-12-01", "2001-01-01", "2001-02-01", "2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01", "2001-07-01", "2001-08-01", "2001-09-01", "2001-10-01", "2001-11-01", "2001-12-01", "2002-01-01", "2002-02-01", "2002-03-01", "2002-04-01", "2002-05-01", "2002-06-01", "2002-07-01", "2002-08-01", "2002-09-01", "2002-10-01", "2002-11-01", "2002-12-01", "2003-01-01", "2003-02-01", "2003-03-01", "2003-04-01", "2003-05-01", "2003-06-01", "2003-07-01", "2003-08-01", "2003-09-01", "2003-10-01", "2003-11-01", "2003-12-01", "2004-01-01", "2004-02-01", "2004-03-01", "2004-04-01", "2004-05-01", "2004-06-01", "2004-07-01", "2004-08-01", "2004-09-01", "2004-10-01", "2004-11-01", "2004-12-01", "2005-01-01", "2005-02-01", "2005-03-01", "2005-04-01", "2005-05-01", "2005-06-01", "2005-07-01", "2005-08-01", "2005-09-01", "2005-10-01", "2005-11-01", "2005-12-01", "2006-01-01", "2006-02-01", "2006-03-01", "2006-04-01", "2006-05-01", "2006-06-01", "2006-07-01", "2006-08-01", "2006-09-01", "2006-10-01", "2006-11-01", "2006-12-01", "2007-01-01", "2007-02-01", "2007-03-01", "2007-04-01", "2007-05-01", "2007-06-01", "2007-07-01", "2007-08-01", "2007-09-01", "2007-10-01", "2007-11-01", "2007-12-01", "2008-01-01", "2008-02-01", "2008-03-01", "2008-04-01", "2008-05-01", "2008-06-01", "2008-07-01", "2008-08-01", "2008-09-01", "2008-10-01", "2008-11-01", "2008-12-01", "2009-01-01", "2009-02-01", "2009-03-01", "2009-04-01", "2009-05-01", "2009-06-01", "2009-07-01", "2009-08-01", "2009-09-01", "2009-10-01", "2009-11-01", "2009-12-01", "2010-01-01", "2010-02-01", "2010-03-01", "2010-04-01", "2010-05-01", "2010-06-01", "2010-07-01", "2010-08-01", "2010-09-01", "2010-10-01", "2010-11-01", "2010-12-01", "2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01", "2011-12-01", "2012-01-01", "2012-02-01", "2012-03-01", "2012-04-01", "2012-05-01", "2012-06-01", "2012-07-01", "2012-08-01", "2012-09-01", "2012-10-01", "2012-11-01", "2012-12-01", "2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01", "2013-09-01", "2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01", "2016-01-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01", "2016-08-01", "2016-09-01", "2016-10-01", "2016-11-01", "2016-12-01", "2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", "2017-06-01", "2017-07-01"]}, "credits": {"enabled": false}, "exporting": {"chartOptions": {"subtitle": null}, "sourceHeight": 900, "sourceWidth": 1600}, "legend": true}
Yet Highcharts renders this like:
Why does it display every single date instead of only showing some like in this example?
I tried changing the format of the dates being returned into a timestamp, used by Javascript's native Date.UTC, but that only makes it look worse:
You can use tickInterval in xAxis.
Here is a working fiddle with your data in it