Skip to content

Instantly share code, notes, and snippets.

@pc-rgundlapalli
Created June 4, 2014 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pc-rgundlapalli/0dd140131d91cc434730 to your computer and use it in GitHub Desktop.
Save pc-rgundlapalli/0dd140131d91cc434730 to your computer and use it in GitHub Desktop.
(function () {
var mapCLArguments,
render,
args = {},
env = {
'D' : 'https://devtrunk.awspcap.com',
'Q' : 'https://qatrunk.awspcap.com',
'S' : 'https://staging.personalcapital.com',
'P' : 'https://home.personalcapital.com'
},
staticUrl,
system = require('system');
mapCLArguments = function () {
var map = {},
i,
key;
for (i = 0; i < system.args.length; i += 1) {
if (system.args[i].charAt(0) === '-') {
key = system.args[i].substr(1, i.length);
map[key] = system.args[i + 1];
}
}
if(typeof map['chd'] == 'undefined'){
console.log('Commandline Usage: -chd data-points');
phantom.exit(1);
}
return map;
};
render = function(params){
var page = require('webpage').create(),
createChart,
renderSVG;
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.onAlert = function (msg) {
console.log(msg);
};
page.onResourceRequested = function (request) {
//console.log('= onResourceRequested()');
//console.log(' request: ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function(response) {
//console.log('= onResourceReceived()' );
//console.log(' id: ' + response.id + ', stage: "' + response.stage + '", response: ' + JSON.stringify(response));
};
page.onLoadStarted = function() {
/*console.log('= onLoadStarted()');
var currentUrl = page.evaluate(function() {
return window.location.href;
});
console.log(' leaving url: ' + currentUrl);*/
};
page.onLoadFinished = function(status) {
//console.log('= onLoadFinished()');
//console.log(' status: ' + status);
};
page.onNavigationRequested = function(url, type, willNavigate, main) {
/*console.log('= onNavigationRequested');
console.log(' destination_url: ' + url);
console.log(' type (cause): ' + type);
console.log(' will navigate: ' + willNavigate);
console.log(' from page\'s main frame: ' + main);*/
};
page.onResourceError = function(resourceError) {
console.log('onResourceError() - unable to load url: "' + resourceError.url + '"');
console.log(' - error code: ' + resourceError.errorCode + ', description: ' + resourceError.errorString );
phantom.exit(1);
};
page.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};
createChart = function(series){
var $container = $('<div>').appendTo(document.body);
$container.attr('id', 'chart-container');
staticUrl = '';
Raphael('chart-container').drawGrid({
max : _.max(series),
min : _.min(series),
topPadding : .1,
bottomPadding : .1,
hideLabels : true
})
.drawLineSeries({
series : series,
type : 'noisyArea',
fill: '#0088cc',
stroke: '#0088cc',
strokeWidth: 2
})
.drawBaseline()
.drawZeroline();
}
renderSVG = function(){
var base64 = page.renderBase64('PNG');
console.log(base64);
}
page.open('about:blank', function (status) {
var svg,
jsfile;
/*// load necessary libraries
for (jsfile in config.files) {
page.injectJs(config.files[jsfile]);
}*/
var series = [], chd = params.chd.split(',');
for(var i = 0; i < chd.length; i++){
series.push(Number(chd[i]));
}
page.includeJs(staticUrl + '/static/bin/scripts/libs/vendor/require.js', function(status){
page.includeJs(staticUrl + '/static/bin/scripts/libs/registry_perspective.js', function(){
page.evaluate(createChart, series);
renderSVG();
phantom.exit();
});
});
});
}
args = mapCLArguments();
if(typeof args.env == 'undefined'){
args.env = 'P';
}
staticUrl = env[args.env];
console.log('staticUrl', staticUrl)
render(args);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment