-
Notifications
You must be signed in to change notification settings - Fork 7
/
charts.js
131 lines (121 loc) · 3.74 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
const { CanvasRenderService } = require('chartjs-node-canvas');
const queryString = require('query-string');
const vsprintf = require("sprintf-js").vsprintf;
const request = require("request");
const smooth = require('array-smooth');
const moment = require("moment");
function getCoinGeckoData(options, callback) {
var queryParams = {
vs_currency: options.vsCurrency,
days: options.days
};
var packetData = {
uri: vsprintf("https://api.coingecko.com/api/v3/coins/smartcrytotech/market_chart?%s", [queryString.stringify(queryParams)]),
method: "GET",
json: true
};
request(packetData, function (error, response, body) {
callback(body);
});
}
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 (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,
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);
}
},
gridLines: {
color: 'rgba(255,255,255,.08)'
}
}],
xAxes: [{
ticks: {
autoSkip: true,
maxTicksLimit: options.xPoints,
maxRotation: 0,
minRotation: 0
},
gridLines: {
color: 'rgba(255, 255, 255, .08)'
},
}]
},
hover: {
mode: 'nearest',
intersect: true
}
}
};
}
const chartCallback = (Chart) => {
// Global config example: https://www.chartjs.org/docs/latest/configuration/
Chart.defaults.global.elements.rectangle.borderWidth = 2;
// Global plugin example: https://www.chartjs.org/docs/latest/developers/plugins.html
Chart.plugins.register({
// plugin implementation
});
// New chart type example: https://www.chartjs.org/docs/latest/developers/charts.html
Chart.controllers.MyType = Chart.DatasetController.extend({
// chart implementation
});
};
(async () => {
const canvasRenderService = new CanvasRenderService(options.width, options.height, chartCallback);
resultCallback(await canvasRenderService.renderToBuffer(makeConfiguration(chartData)));
})();
}
module.exports = {
getPriceChart: function (options, resultCallback) {
getCoinGeckoData(options, function (data) {
getCustomChart(options, data.prices, resultCallback);
});
},
getVolumeChart: function (options, resultCallback) {
getCoinGeckoData(options, function (data) {
getCustomChart(options, data.total_volumes, resultCallback);
});
},
getMarketcapChart: function (options, resultCallback) {
getCoinGeckoData(options, function (data) {
getCustomChart(options, data.market_caps, resultCallback);
});
}
};