I have some records for that I am using three arrays which contains dynamic values.
var Results=new Array();
var Second=new Array();
var First=new Array();
I want to show these array values in Google chart but as per Google chart they shows array values differently.
How to add my arrays into Google Chart ?
If they are all the same length, you can try something like this:
var Combined = new Array();
Combined[0] = ['Results', 'First', 'Second'];
for (var i = 0; i < Results.length; i++){
Combined[i + 1] = [ Results[i], First[i], Second[i] ];
}
//second parameter is false because first row is headers, not data.
var table = google.visualization.arrayToDataTable(Combined, false);
See here for documentation on array to DataTable.
Related
new to javascript. I am trying to make a new series in Highcharts using one already graphed series multiplied by another already graphed series. How do I add the new series with its own y-axis? I am using some base code that I have not done myself.
ChartOptions.Series[index].data holds an array of arrays: so for example :[1563480488000, 12.144] would be timestamp and datavalue for every data point in the series.
So I would want something like this:
[1563480488000, 12.144] * [1563480488000, 1.0] = [1563480488000, 12.144]
that's 12.144*1.0, and the timestamp is kept.
dynamicChart is a new Highcharts.StockChart(chartOptions) its just away from the snippet I am working with.
I've tried the following code.
var voltSeries = chartOptions.series[0].data;
var ampSeries = chartOptions.series[1].data;
var wattSeries = [];
for(var i = 0; i <= voltSeries.length; i++){
var valu = chartOptions.series[1].data[i,1]*chartOptions.series[0].data[i,1];
wattSeries[i,1] = valu;
wattSeries[i,0] = valu;
}
eval('window.console && console.log(voltSeries)');
eval('window.console && console.log(wattSeries)');
dynamicChart.addSeries({
name: 'Watts',
data: wattSeries
});
dynamicChart.redraw();
the output of wattSeries is an Array of (2) [NaN, NaN] but It should be the timestamp and the multiplied value.
and It seems like I cannot graph it no matter what I do.
The problem is incorrectly getting values from the array. For nested arrays you should use several parentheses: array[x][x]
var dynamicChart = Highcharts.chart('container', chartOptions);
var voltSeries = chartOptions.series[0].data,
ampSeries = chartOptions.series[1].data,
wattSeries = [],
valu;
for (var i = 0; i < voltSeries.length; i++) {
valu = voltSeries[i][1] * ampSeries[i][1];
wattSeries.push([ampSeries[i][0], valu]);
}
dynamicChart.addSeries({
name: 'Watts',
data: wattSeries,
yAxis: 1
});
Live demo: http://jsfiddle.net/BlackLabel/vpzLou6r/
API Reference: https://api.highcharts.com/class-reference/Highcharts.Chart#addSeries
For some hours now I'm trying to get the model data of selected rows of a tree table.
I used this example: https://openui5.hana.ondemand.com/#/sample/sap.ui.table.sample.TreeTable.JSONTreeBinding/preview
Additionally, I added sortProperty and filterProperty to the columns. Until now everything works.
What I want to do is to submit the json data via ajax of all selected rows. for this, I need to get the json data of the selected rows.
What I tried:
var oTable = this.getView().byId("tableName").getSelectedIndicies()
and then
for(var i=0; i<=oTable.length; i++) {
this.getView().byId("tableName").getModel().getData().jobs[oTable[i]]
}
it seems that when I use the sorter and filter function, the indicies are not correct anymore. The indicies keys won't change.
any idea how to solve my request? thx in advance!
There is a small change you can do to get the right data in your for loop.
Get the bindingContext of the item which is selected as per the indices information.
var sPath = TreeTable.getRows()[0].getBindingContext()
Get the data from the model as:
oTreeTable.getModel().getProperty(sPath)
this is how I solved it:
var oJSON = {};
var aData = [];
var oTable = this.getView().byId("TreeTable");
var aIndicies = oTable.getSelectedIndices();
var oSelect = this.getView().byId("selectStandort").getSelectedKey();
for (var i=0; i<aIndicies.length; i++) {
var oTableContext = oTable.getContextByIndex(aIndicies[i]);
var rowData = oTable.getModel("jobs").getProperty(oTableContext.getPath());
aData.push(rowData);
}
oJSON.jobs = aData;
oJSON.standort = oSelect;
I have a multidimensional array created by taking data from a google spreadsheet. I am attempting to seperate out the data based on results in a specific column. For example:
var j = {
["Mine", "House"],
["Your", "House"],
["his", "apt"]
}
Given that we want to seperate by column 2. We should get in theory:
var new = {
[["Mine", "House"] , ["Your", "House"]],
[["his", "apt"]]
}
Two entries being a house, and 1 being an apartment. I am haveing a huge issue with treating each entry as its own obj. Assuming it is even possible. I guess we would access specific parts of each object like so, new[0][1][1]? This obviously shouldnt work like this. Is there another way to seperate the entries in the way I am attempting? As it stands right now, I believe my code is just creating the same number of rows as in the original data.
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.insertSheet("Report");
var data_sheet =spreadsheet.getSheetByName("Data");
var genCounsel_data = data_sheet.getRange(240, 1, 96, 7).getValues(); //get genCounseling data
var report_sheet = spreadsheet.getSheetByName("Report");
//setup key values for columns in report sheet
report_sheet.appendRow(["Student Service", "Unit Student Service Outcome", "Indicators Data Sources", "How indicator was measured", "Benchmark Data", "Assigned to do Assessment", "Email"])
//seperate out by SS outcomes
var genCounselDataByOutcomes = new Array(new Array()); //all responses for each outcome, also parrellel
var outcomes_freq = new Array(); //parrellel arrays
var found = false;
//get services and frequency;
for(var i=0; i<genCounsel_data.length; ++i){
genCounsel_data[i][OUTCOMES_COL].toString().toLowerCase();
for(var j=0; j<genCounselDataByOutcomes.length && !found; ++j){
if(genCounselDataByOutcomes[j][OUTCOMES_COL] == genCounsel_data[i][OUTCOMES_COL]){
genCounselDataByOutcomes[j].push(genCounsel_data[i]); //ADD to row with same outcomes
++outcomes_freq[j]; //update amount of entries in said outcome
found = true;
}
}
if(found == false){
genCounselDataByOutcomes.push(new Array);
genCounselDataByOutcomes[genCounselDataByOutcomes.length-1].push([genCounsel_data[i]]);
outcomes_freq.push(1);
}
else
found = false;
}
for(var i=0; i<outcomes_freq.length;++i)
Logger.log(outcomes_freq[i]);
//for each outcome select a random one and move entire row to sheet;
for(var i=0; i<genCounselDataByOutcomes.length; ++i){
Logger.log(genCounselDataByOutcomes[i]);
}
QUESTION:
How can I seperate my data as multiple objects in a row of an array and be able to access specific components of each entry as shown in my example above? If this is not exactly possible in this way, is there another solution to this issue?
First of all, your j and new (which by the way is not a valid var name) need a key if they are objects or to be used as array, like below:
var j = [
["Mine", "House"],
["Your", "House"],
["his", "apt"]
];
var newVar = [
[["Mine", "House"] , ["Your", "House"]],
[["his", "apt"]]
];
That said and fixed, you can iterate over your array of arrays and use the column you want to use as filter to get the unique values to be used to group the final result.
Here is the final result:
var j = [
["Mine", "House"],
["Your", "House"],
["his", "apt"]
];
var uniqueValues = [];
var indexColumn = 1; //Here you specify the column you want to use to filter/group the elements from j
j.forEach(function(element){
if(!uniqueValues.includes(element[indexColumn])){
uniqueValues.push(element[indexColumn]);
}
});
//Now you use the `uniqueValues` to filter the `j` array and generate the `groupedArrays` array, which is the one you are looking for:
var groupedArrays = [];
uniqueValues.forEach(function(uniqueValue){
groupedArrays.push(j.filter(function(element){
return element[indexColumn] === uniqueValue;
}));
});
console.log(groupedArrays);
I hope this helps you.
Good luck.
in my API, i have few things like suppose name of companies and am printing them in sorted form after fetching from api but their crrosponding URL'sarent getting printed corrosponding to the name of companies
i.e, i have sorted the keys in the object and printed them in ascending order but the values of those keys arent printing corrosponding to those keys
the URL's are getting printed in the same sequence as its there in API but the keys, i have sorted them to print in accending order
am not able to print the values corrosponding to their keys from the object
the function is here:
function fetchFromApi() {
var url = '<My API from where am fetching the data>';
var urlResponse = UrlFetchApp.fetch(url);
var urlResult = JSON.parse(urlResponse);
var key = Object.keys(urlResult);
var tempArr = [];
for (var x in urlResult) {
var value = urlResult[x];
value = value.replace(/\\/g, '');
tempArr.push([value])
}
inputSheet.getRange(2,6,tempArr.length,1).setValues(tempArr);
printData();
}
function printData() {
keys.sort();
key = [];
for (var i=0; i<keys.length; i++) {
key[i] = [];
key[i][0] = keys[i];
}
var range = inputSheet.getRange(2, 1, key.length, 1);
range.setValues(key);
}
You could try writing the keys and values to the sheet and then sort using range.sort(col).
In the for loop in fetchFromApi(), after tempArr.push([value]) do key.push([x]). At the end of the loop, write key to column 1 and tempArr to column 6 of inputSheet. Then, you can do
var range = inputSheet.getRange(2, 1, key.length, 6);
range.sort(1);
which sorts everything between columns 1 and 6 in ascending order according to the value of the keys in column 1. See below for the reference on range.sort(sortSpecObj).
https://developers.google.com/apps-script/reference/spreadsheet/range#sortsortspecobj
I'm trying to get the search terms and their values in the rising table here:
http://www.google.com/trends/explore#cat=0-14&date=today%207-d&cmpt=q
I can't work out what html tag/class/path they're in. How can I work it out? I tried looking at the source code but it wasn't much help.
Any help is really appreciated - Thx! Antoine
The following snippet will return an array of arrays (the overarching array contains values for each table in the page). Each array element has an array where each table row is an object, broken down into it's 'term' and 'value'.
var tableValues = [];
var t = document.querySelectorAll(".trends-table-data");
if(t.length>0){
var rows, row, cells, values;
for(var i=0; i<t.length; i++){
values = [];
rows = t[i].getElementsByTagName("tr");
for(var r=0; r<rows.length; r++){
row = rows[r];
if(row.className.indexOf('trends-table-row')===-1) continue;
cells = row.getElementsByTagName("td");
values.push({
term: cells[0].innerText.replace(/^\s+|\s+$/g, ''),
value: cells[1].innerText.replace(/^\s+|\s+$/g, '')
});
}
tableValues[i] = values;
}
console.log(tableValues);
}
Since there are two tables on the page, the output for the page you're referencing is:
tableValues = [[{"term":"friv","value":"100"},{"term":"baby","value":"55"},{"term":"hot","value":"50"},{"term":"girls","value":"45"},{"term":"games","value":"45"},{"term":"juegos","value":"30"},{"term":"العاب","value":"25"},{"term":"love","value":"25"},{"term":"bible","value":"20"},{"term":"india","value":"20"}],[{"term":"sophiya haque","value":"Breakout"},{"term":"temple run 2","value":"+3,200%"},{"term":"крещение","value":"+700%"},{"term":"dear abby","value":"+450%"},{"term":"temple run","value":"+200%"},{"term":"amber heard","value":"+130%"},{"term":"plein champ","value":"+130%"},{"term":"paranormal activity 4","value":"+90%"},{"term":"scientology","value":"+70%"},{"term":"mama","value":"+60%"}]]