-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchart.js
More file actions
72 lines (63 loc) · 1.7 KB
/
chart.js
File metadata and controls
72 lines (63 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require('dotenv').config();
var plotly = require('plotly')(process.env.PLOTLY_USERNAME, process.env.PLOTLY_PASSWORD);
var cloudinary = require('cloudinary');
var fs = require('fs');
const rn = require('random-number');
cloudinary.config({
cloud_name: process.env.cloud_id,
api_key: process.env.cloudinary_api_key,
api_secret: process.env.cloudinary_app_secret
});
function grapher(stock, data, params, callback) {
var xData = [];
var yData = [];
console.log(params)
for (var day in data) {
xData.push(day);
yData.push(data[day][params["dp"]]);
}
var trace1 = {
x: xData,
y: yData,
type: "scatter"
};
if (params.length) {
params.title = params.title + " ("+ params.length + ")";
}
var figure = { 'data': [trace1], "layout" : {"title" : params.title}};
var imgOpts = {
format: 'png',
width: 1000,
height: 500,
"layout": {
"title": "Stock"
}
};
//plot image
plotly.getImage(figure, imgOpts, function (error, imageStream) {
if (error) callback(error, null);
if (!imageStream) callback("ERROR (plotly.getImage) imageStream is null, aborting", null);
var options = {
min: 1
, max: 980
, integer: true
}
var name = "./output/" + rn(options) + ".png";
var fileStream = fs.createWriteStream(name)
.on('finish', () => upload());
imageStream.pipe(fileStream);
//upload image
function upload() {
cloudinary.uploader.upload(name, function(result) {
if (result) {
callback(null, result.url);
} else {
callback("ERROR (grapher) result is set to null from cloudinary", null);
}
});
}
});
}
module.exports = {
grapher : grapher
}