javascript plotting realtime - javascript

I need realtime plotting in javascript, and I came across this:
http://nme.pl/en/2011/01/real-time-graph-using-javascript
This is close to what i want. Now that my javascript understanding is limited, can any guru point me where from this canvas is getting data? Is it taking from any online url? What is that google analytics url about?

The post is old but came up when googling so I thought I'd post some links to more modern solutions.
Rickshaw is getting some good reviews - https://github.com/shutterstock/rickshaw
Smoothie is also getting some good reviews and I've seen it in action - https://github.com/joewalnes/smoothie
HTH

That particular example is getting data from a JavaScript function that generates "random" data. Code is:
// rotating across pseudo-random values array
var Rotator = function() {
var self = this;
self.data = [
16,23,41,38,45,31,29,22,27,22,25,29,32,45,88,30,
26,22,45,23,36,48,99,22,32,26,22,27,5,0,0,3,15
];
self.index = 0;
self.single = function() {
self.index += 1;
if (self.index > self.data.length) {
self.index = 1;
}
return self.data[self.index-1];
};
self.many = function(length) {
var results = [];
for (var i=0;i<length;i++) {
results.push(self.single());
}
return results;
};
};
As mentioned in comments, you can look at Flor or Ext-JS (Sencha).

Related

How to create a bookmarklet with multiple functions

I'm new to creating JavaScript bookmarklets but have got a certain way in solving my problems but have got stuck on one final bit.
Basically, I want to create a bookmarklet that will replace text in 2 places in the URL - the subdomain and the URI.
I have managed to do this for the first part:
(function() {
window.location = window.location
.toString()
.replace(/^https:\/\/www\./, "https://edit.");
})();
Next, I need to grab some metadata (cab-id) from the page. I've managed to do this an print it to the console:
function getCabID() {
var metas = document.getElementsByTagName("meta");
for (var i = 0; i < metas.length; i++) {
if (metas[i].getAttribute("name") == "cab-id") {
return metas[i].getAttribute("content");
}
}
return "";
}
console.log(getCabID());
The next thing I need to do is replace the end of the url (everything from "www.xxxxxx.org.uk/*" with the following:
/EPiServer/CMS/Home#context=epi.cms.contentdata:///
I can't figure out how to do this, I'm really struggling. I've come up with the following but it's not working:
(function() {
var url=window.location.href;
stringUrl=String(url);
stringUrl=stringUrl.replace(/^https:\/\/www.xxxxxx.org.uk\/, "https://edit.xxxxxx.org.uk/EPiServer/CMS/Home#context=epi.cms.contentdata:///");
document.location=stringUrl;
})();
I'll also need to pop the cab-id at the end of all this directly after ///.
Sorry for the long question but what I need to do is:
Make the 3rd one actually work!
Combine the 3 functions
Any tips would be massively appreciated :D
As I understood your question, the following bookmarklet probably allows to combine the 2nd and 3rd steps:
javascript:(function() {
window.location.href = "https://edit.xxxxxx.org.uk/EPiServer/CMS/Home#context=epi.cms.contentdata:///" + getCabID();
function getCabID() {
var metas = document.getElementsByTagName("meta");
for (var i = 0; i < metas.length; i++) {
if (metas[i].getAttribute("name") == "cab-id") {
return metas[i].getAttribute("content");
}
}
return "";
}
})();

How to retrieve Rows Values on Google analytics API V4 - JavaScript

I'm trying to migrate my tiny report from V3 to V4, but I have found an issue which is annoying me and making me feel like I'm totally dumb.
So I just took the example code at https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js
and change a couple of things, and it works, it runs the report. But when I try to retrieve the data from the different rows with the below function:
function displayResults(response) {
var Objeto = response.result["reports"];
var Filas01 = Objeto["data"];
console.log(Objeto);
console.log(Filas01);
}
Objeto shows everything within .reports
But Filas01 shows undefined, I have tried to retrieve reponse.results.reports.data.rows;
And several variations but it says undefined all the time,
I have no clue why it was working on V3 and is not on V4,
Please any help would be much appreciated :)
There are samples of how to make requests and precess the response in various languages. But specifically here is a simple JavaScript function which processes the results into a table:
function handleReportingResults(response) {
if (!response.code) {
outputToPage('Query Success');
for( var i = 0, report; report = response.reports[ i ]; ++i )
{
output.push('<h3>All Rows Of Data</h3>');
if (report.data.rows && report.data.rows.length) {
var table = ['<table>'];
// Put headers in table.
table.push('<tr><th>', report.columnHeader.dimensions.join('</th><th>'), '</th>');
table.push('<th>Date range #</th>');
for (var i=0, header; header = report.columnHeader.metricHeader.metricHeaderEntries[i]; ++i) {
table.push('<th>', header.name, '</th>');
}
table.push('</tr>');
// Put cells in table.
for (var rowIndex=0, row; row = report.data.rows[rowIndex]; ++rowIndex) {
for(var dateRangeIndex=0, dateRange; dateRange = row.metrics[dateRangeIndex]; ++dateRangeIndex) {
// Put dimension values
table.push('<tr><td>', row.dimensions.join('</td><td>'), '</td>');
// Put metric values for the current date range
table.push('<td>', dateRangeIndex, '</td><td>', dateRange.values.join('</td><td>'), '</td></tr>');
}
}
table.push('</table>');
output.push(table.join(''));
} else {
output.push('<p>No rows found.</p>');
}
}
outputToPage(output.join(''));
} else {
outputToPage('There was an error: ' + response.message);
}
}
I would also recommend taking the time to review the overal structure of the response in the reference docs

javascript functions wait for data availability or variable not capable of handling huge data

forgive the trivial question but I am more used to C++ and Python code than javascript.
I have the following code from the THREE JS PLY loader:
var geometry;
var scope = this;
if (data instanceof ArrayBuffer) {
geometry = isASCII(data) ? parseASCII(bin2str(data)) : parseBinary(data);
} else {
geometry = parseASCII(data);
}
parse: function (data) {
function isASCII(data) {
var header = parseHeader(bin2str(data));
return header.format === 'ascii';
}
function bin2str(buf) {
var array_buffer = new Uint8Array(buf);
var str = '';
for (var i = 0; i < buf.byteLength; i++) {
str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
}
return str;
}
It works fine if I load a small ply file but browser crashes on very large one. I believe there are two "possible" issues:
1) on a large file the string str returned by the function bin2str(buf) might not be able to handle the parsing process
2) in the function isASCII(data) the line
parseHeader(bin2str(data));
crashes the browser as the bin2str(data) cannot return a proper value in time as the process is very memory consuming
I am using the conditional as i am not totally sure of what the problem is. Any suggestion and/or possible solution?
Thank you,
Dino
In the end the solution I have adopted was to decimate my file using the free software MeshLab.
Hope this helps.
Dino

How to set the positions of dynamicaly loaded elements using Javascript?

I am working on an application that loads in "Apps" from a file on the server. At the moment i have to load the apps in a specific order otherwise they will have the wrong positions in the array of positions.
Here is the code to help explain what i mean.
function getFacebook() {
var appname = "facebooka1thd";
$.get("apps/"+appname+"/"+appname+".html", function(data){
$('.AppList').append(data);
$.cookie(appname, 1, { expires : 365 });
checkpositions();
});
};
function checkpositions() {
if ($.cookie('PosApps')){
var Poscookie = $.cookie('PosApps');
var Pos = JSON.parse(Poscookie);
$("#User").css({top:Pos[0].top, left:Pos[0].left});
$("#facebooka1thd").css({top:Pos[1].top, left:Pos[1].left});
$("#youtubea2thd").css({top:Pos[2].top, left:Pos[2].left});
};
};
function getAppPositions() {
var apps = $(".App"),
positions = [];
$.each(apps, function (index, app) {
var positionInfo = $(app).position();
positions.push(positionInfo);
console.log(positionInfo);
});
var setPositions = JSON.stringify(positions);
$.cookie("PosApps", setPositions, { expires : 365 });
};
I would like the code to adapt to the number off apps present and the order in which they are added or removed.
basically i dont have a clue how to get around this -_- I think that most of my code would have to change in-order for this to be possible because at the moment the positions are saved in the order in which the apps are added but that wont help when the user removes one of the apps and also the positions are being set relative to the order of the apps being added.
Any help with this would be great!
I managed to work it out for my self...
function checkpositions() {
if ($.cookie('PosApps')){
var Poscookie = $.cookie('PosApps');
var Pos = JSON.parse(Poscookie);
for (var i = 0; i < Pos.length; i++) {
$("#"+Pos[i][0]).css({top:Pos[i][1].top, left:Pos[i][1].left});
};
};
};
function getAppPositions() {
var apps = $(".App"),
positions = [];
$.each(apps, function (index, app) {
var id = $(app).attr('id');
var positionInfo = [id,$(app).position()];
positions.push(positionInfo);
console.log(positionInfo);
});
var setPositions = JSON.stringify(positions);
$.cookie("PosApps", setPositions, { expires : 365 });
};
All i had to do was make a 2D array with the id of the element before its position and set the CSS using Pos[i][0] for the id and Posi.top/left for the positions. Hope this helps anyone who gets stuck with something like this. Also if you do get stuck with a problem i would recommend going to this site. Their tutorials are really good and are what helped me figure this problem out.

Need to list all the sites under a google domain using Google Apps Script

I need to list all the sites under a google domain using Google Apps Script. I have written a small script, but it only returns the sites I have created:
function list_sites() {
var doc = DocumentApp.create('SiteList1');
var domain = 'test.com';
var sites = SitesApp.getSites(domain);
doc.appendParagraph("Top");
doc.appendParagraph(sites);
doc.appendParagraph("Bottom");
doc.saveAndClose(); // Save and close the document
}
How do I get all the sites?
You can use the getAllSites method.
SitesApp.getAllSites(domain, start, max)
I'm not sure why it's seemingly undocumented and not in the API doc but you can find references to it and I have production scripts that use it daily.
The example Peter mentioned, did not work for me, see also this question: Why does SitesApp.getSites(myDomain) not work?
But I found a solution which also might help you:
function list_sites() {
var doc = DocumentApp.create('SiteList1');
var domain = 'example.com';
var site = SitesApp.getSite(domain);
doc.appendParagraph("Top");
var pages = site.getAllDescendants();
for (i in pages) {
doc.appendParagraph(pages[i].getTitle());
}
doc.appendParagraph("Bottom");
doc.saveAndClose(); // Save and close the document
}
https://gist.github.com/eduvik/46bb72f3ffea57402b3f
Needs to be run as an admin user
function listSites() {
var domain = UserManager.getDomain();
var PAGE_LENGTH=200;
sheet = SpreadsheetApp.getActiveSheet();
var sites = SitesApp.getAllSites(domain,0,PAGE_LENGTH);
for(var i=0; sites.length != 0; i+=PAGE_LENGTH){
for (var j=0; j<sites.length; j++) {
sheet.appendRow([sites[j].getUrl()]);
}
sites = SitesApp.getAllSites(domain, i, PAGE_LENGTH);
}
};

Categories