forked from CryptoLover705/SmartCryptoTech-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.js
140 lines (127 loc) · 3.37 KB
/
charts.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ChartJSNodeCanvas } from "chartjs-node-canvas";
import * as utils from "./utils.js";
import smooth from "array-smooth";
import moment from "moment";
import axios from "axios";
const request = axios.create({
timeout: 10000, // 10 seconds
headers:{
'User-Agent': 'Conceal Services'
}
});
function getCoinGeckoData(options, callback) {
var queryParams = {
vs_currency: options.vsCurrency,
days: options.days
};
request.get(utils.geckoURL("coins/fango/market_chart", queryParams)).then(response => {
callback(response.data);
}).catch(err => {
console.log(`getCoinGeckoData: ${err.message}`);
callback(null);
});
}
function getCustomChart(options, chartData, resultCallback) {
function makeConfiguration(data) {
var timeLabels = [];
var dataPoints = [];
var dataLength = data.length;
var durationAsMS = moment.duration(options.days / dataLength, 'd').asMilliseconds();
data.forEach(function (value) {
dataPoints.push(value[1]);
});
for (let i = dataLength - 1; i >= 0; i--) {
timeLabels.push(moment().subtract(durationAsMS * (i + 1), 'ms').format(options.dateFormat));
}
return {
type: 'line',
data: {
labels: timeLabels,
datasets: [{
data: smooth(dataPoints, 3),
backgroundColor: 'rgba(255,165,0,0.2)',
fill: true,
borderWidth: 2,
pointRadius: 0,
borderColor: '#FFA500'
}]
},
options: {
animation: false,
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false,
labels: {
display: false
}
}
},
scales: {
yAxes: {
ticks: {
maxTicksLimit: 5,
fontSize: 10,
callback: function (value, index, values) {
return options.priceSymbol + value.toFixed(options.priceDecimals);
}
},
grid: {
display: false
}
},
xAxes: {
ticks: {
autoSkip: true,
maxTicksLimit: options.xPoints,
maxRotation: 0,
minRotation: 0
},
grid: {
display: false
}
}
},
hover: {
mode: 'nearest',
intersect: true
}
}
};
}
(async () => {
const canvasRenderService = new ChartJSNodeCanvas({
width: options.width,
height: options.height,
});
resultCallback(await canvasRenderService.renderToBuffer(makeConfiguration(chartData)));
})();
}
export function getPriceChart(options, resultCallback) {
getCoinGeckoData(options, function (data) {
if (data) {
getCustomChart(options, data.prices, resultCallback);
} else {
resultCallback(null);
}
});
};
export function getVolumeChart(options, resultCallback) {
getCoinGeckoData(options, function (data) {
if (data) {
getCustomChart(options, data.total_volumes, resultCallback);
} else {
resultCallback(null);
}
});
};
export function getMarketcapChart(options, resultCallback) {
getCoinGeckoData(options, function (data) {
if (data) {
getCustomChart(options, data.market_caps, resultCallback);
} else {
resultCallback(null);
}
});
};