From 01aa8869433215ce8c7c9591d791a8fd7b707d6a Mon Sep 17 00:00:00 2001 From: Redume Date: Fri, 2 Aug 2024 15:41:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=84=D0=B8=D0=BA=D0=B0=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=20chartjs-image?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chart/.gitignore | 1 + chart/main.js | 67 +++++++++++++++++++++++++++++++++++++++++ chart/package-lock.json | 44 +++++++++++++++++++++++++++ chart/package.json | 22 ++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 chart/.gitignore create mode 100644 chart/main.js create mode 100644 chart/package-lock.json create mode 100644 chart/package.json diff --git a/chart/.gitignore b/chart/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/chart/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/chart/main.js b/chart/main.js new file mode 100644 index 0000000..16a38d4 --- /dev/null +++ b/chart/main.js @@ -0,0 +1,67 @@ +const ChartJSImage = require('chart.js-image'); +const pool = require('../database/postgresql.js'); + +async function gen_chart(from_currency, conv_currency, start_date, end_date) { + const data = await pool.query('SELECT date, rate FROM currency WHERE ' + + '(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2 ORDER BY date ', [ + from_currency.toUpperCase(), + conv_currency.toUpperCase(), + start_date, + end_date + ]); + + if (!data) return new Error('Missing data'); + + const date = []; + const rate = []; + + for (let i = 0; i < data.rows.length; i++) { + date.push(data.rows[i].date.toLocaleDateString()); + rate.push(data.rows[i].rate); + } + + const chart = ChartJSImage().chart({ + type: 'line', + options: { + title: { + display: true, + text: `${from_currency} / ${conv_currency}`, + }, + }, + data: { + labels: date, + datasets: [ + { + label: 'rate', + borderColor: rate[rate.length-2] < rate[rate.length-1] ? 'rgb(24, 218, 39)' : 'rgb(243, 85, 50)', + backgroundColor: rate[rate.length-2] < rate[rate.length-1] ? 'rgb(36, 175, 47)' : 'rgb(218, 56, 24)', + data: rate, + borderWidth: 2, + }, + ], + }, + scales: { + xAxes: [ + { + scaleLabel: { + display: true, + labelString: 'Day' + }, + }, + ], + yAxes: [ + { + stacked: false, + scaleLabel: { + display: true, + labelString: 'Rate' + }, + }, + ], + }, + }).width(1000).height(1000); + + return chart.toURL(); +} + +module.exports = { gen_chart } \ No newline at end of file diff --git a/chart/package-lock.json b/chart/package-lock.json new file mode 100644 index 0000000..8a9fb20 --- /dev/null +++ b/chart/package-lock.json @@ -0,0 +1,44 @@ +{ + "name": "chart", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "chart", + "version": "1.0.0", + "license": "GPL-3.0-or-later", + "dependencies": { + "chart.js-image": "^6.1.3" + } + }, + "node_modules/chart.js-image": { + "version": "6.1.3", + "resolved": "https://registry.npmjs.org/chart.js-image/-/chart.js-image-6.1.3.tgz", + "integrity": "sha512-K+h0dc/Wf/Dk5CWKrV7xxS7ozONCiQ73XL+QStBxUpgABzSblJwQe/R6X1RWxY2Z/G8OhKsPFB0HC7bwiCmB2w==", + "license": "MIT", + "dependencies": { + "javascript-stringify": "2.0.1", + "node-fetch": "2.6.0" + }, + "engines": { + "node": ">12" + } + }, + "node_modules/javascript-stringify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/javascript-stringify/-/javascript-stringify-2.0.1.tgz", + "integrity": "sha512-yV+gqbd5vaOYjqlbk16EG89xB5udgjqQF3C5FAORDg4f/IS1Yc5ERCv5e/57yBcfJYw05V5JyIXabhwb75Xxow==", + "license": "MIT" + }, + "node_modules/node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", + "license": "MIT", + "engines": { + "node": "4.x || >=6.0.0" + } + } + } +} diff --git a/chart/package.json b/chart/package.json new file mode 100644 index 0000000..441b281 --- /dev/null +++ b/chart/package.json @@ -0,0 +1,22 @@ +{ + "name": "chart", + "version": "1.0.0", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/redume/kekkai.git" + }, + "author": "Redume", + "license": "GPL-3.0-or-later", + "bugs": { + "url": "https://github.com/redume/kekkai/issues" + }, + "homepage": "https://github.com/redume/kekkai#readme", + "description": "A service for creating graphs", + "dependencies": { + "chart.js-image": "^6.1.3" + } +}