about summary refs log tree commit diff
path: root/users/wpcarro/ynabsql/dataviz/chart.js
diff options
context:
space:
mode:
Diffstat (limited to 'users/wpcarro/ynabsql/dataviz/chart.js')
-rw-r--r--users/wpcarro/ynabsql/dataviz/chart.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/users/wpcarro/ynabsql/dataviz/chart.js b/users/wpcarro/ynabsql/dataviz/chart.js
new file mode 100644
index 0000000000..7ec8f00aae
--- /dev/null
+++ b/users/wpcarro/ynabsql/dataviz/chart.js
@@ -0,0 +1,66 @@
+const colors = {
+  red: 'rgb(255, 45, 70)',
+  green: 'rgb(75, 192, 35)',
+};
+
+////////////////////////////////////////////////////////////////////////////////
+// Main
+////////////////////////////////////////////////////////////////////////////////
+
+const mount = document.getElementById('mount');
+
+const chart = new Chart(mount, {
+  type: 'scatter',
+  data: {
+    datasets: [
+      {
+        label: 'Revenue',
+        data: data.data.transactions.filter(x => x.Inflow > 0).map(x => ({
+          x: x.Date,
+          y: x.Inflow,
+          metadata: x,
+        })),
+        backgroundColor: colors.green,
+      },
+      {
+        label: 'Expenses',
+        data: data.data.transactions.filter(x => x.Outflow).map(x => ({
+          x: x.Date,
+          y: x.Outflow,
+          metadata: x,
+        })),
+        backgroundColor: colors.red,
+      },
+    ],
+  },
+  options: {
+    scales: {
+      x: {
+        type: 'time',
+        title: {
+          display: true,
+          text: 'Date',
+        },
+      },
+      y: {
+        title: {
+          display: true,
+          text: 'Amount ($USD)'
+        },
+      },
+    },
+    plugins: {
+      tooltip: {
+        callbacks: {
+          title: function(x) {
+            return `$${x[0].raw.y} (${x[0].raw.metadata.Date.toLocaleDateString()})`;
+          },
+          label: function(x) {
+            const { Category, Payee, Memo } = x.raw.metadata;
+            return `${Payee} - ${Category} (${Memo})`;
+          },
+        },
+      },
+    },
+  },
+});