mirror of
https://github.com/jcreek/BankStatementAnalyser.git
synced 2026-07-15 03:53:47 +00:00
feat(*): Improve styling of chart
This commit is contained in:
Generated
+11
@@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/chart.js": "^2.9.37",
|
"@types/chart.js": "^2.9.37",
|
||||||
"chart.js": "^4.1.0",
|
"chart.js": "^4.1.0",
|
||||||
|
"chroma-js": "^2.4.2",
|
||||||
"html-webpack-plugin": "^5.5.0",
|
"html-webpack-plugin": "^5.5.0",
|
||||||
"react-dev-utils": "^12.0.1",
|
"react-dev-utils": "^12.0.1",
|
||||||
"webpack-manifest-plugin": "^5.0.0",
|
"webpack-manifest-plugin": "^5.0.0",
|
||||||
@@ -3328,6 +3329,11 @@
|
|||||||
"node": ">= 6"
|
"node": ">= 6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/chroma-js": {
|
||||||
|
"version": "2.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/chroma-js/-/chroma-js-2.4.2.tgz",
|
||||||
|
"integrity": "sha512-U9eDw6+wt7V8z5NncY2jJfZa+hUH8XEj8FQHgFJTrUFnJfXYf4Ml4adI2vXZOjqRDpFWtYVWypDfZwnJ+HIR4A=="
|
||||||
|
},
|
||||||
"node_modules/chrome-trace-event": {
|
"node_modules/chrome-trace-event": {
|
||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz",
|
||||||
@@ -13442,6 +13448,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"chroma-js": {
|
||||||
|
"version": "2.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/chroma-js/-/chroma-js-2.4.2.tgz",
|
||||||
|
"integrity": "sha512-U9eDw6+wt7V8z5NncY2jJfZa+hUH8XEj8FQHgFJTrUFnJfXYf4Ml4adI2vXZOjqRDpFWtYVWypDfZwnJ+HIR4A=="
|
||||||
|
},
|
||||||
"chrome-trace-event": {
|
"chrome-trace-event": {
|
||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz",
|
||||||
|
|||||||
@@ -40,6 +40,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/chart.js": "^2.9.37",
|
"@types/chart.js": "^2.9.37",
|
||||||
"chart.js": "^4.1.0",
|
"chart.js": "^4.1.0",
|
||||||
|
"chroma-js": "^2.4.2",
|
||||||
"html-webpack-plugin": "^5.5.0",
|
"html-webpack-plugin": "^5.5.0",
|
||||||
"react-dev-utils": "^12.0.1",
|
"react-dev-utils": "^12.0.1",
|
||||||
"webpack-manifest-plugin": "^5.0.0",
|
"webpack-manifest-plugin": "^5.0.0",
|
||||||
|
|||||||
+21
-21
@@ -1,5 +1,6 @@
|
|||||||
import { transactions } from "./bankData";
|
import { transactions } from "./bankData";
|
||||||
import Chart from 'chart.js/auto';
|
import Chart from 'chart.js/auto';
|
||||||
|
import * as chroma from 'chroma-js';
|
||||||
|
|
||||||
export default function generateCharts(): void {
|
export default function generateCharts(): void {
|
||||||
// const filter: Filter = { year: 2022, category: 'Eating out', excludeIncome: true};
|
// const filter: Filter = { year: 2022, category: 'Eating out', excludeIncome: true};
|
||||||
@@ -41,6 +42,13 @@ function getChartData(transactions: BankTransaction[], filters: { year?: number,
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate a color scale using the chroma-js library, with the number of colors equal to the number of categories
|
||||||
|
const categories = new Set<string>();
|
||||||
|
for (const transaction of transactions) {
|
||||||
|
categories.add(transaction.category);
|
||||||
|
}
|
||||||
|
const colorScale = chroma.scale('Spectral').mode('hsl').colors(categories.size);
|
||||||
|
|
||||||
// Filter the transactions based on the specified filters
|
// Filter the transactions based on the specified filters
|
||||||
const filteredTransactions = transactions.filter(transaction => {
|
const filteredTransactions = transactions.filter(transaction => {
|
||||||
if (filters.year && transaction.dateYear !== filters.year) {
|
if (filters.year && transaction.dateYear !== filters.year) {
|
||||||
@@ -90,40 +98,32 @@ function getChartData(transactions: BankTransaction[], filters: { year?: number,
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Add a dataset for each category
|
// Add a dataset for each category
|
||||||
const categories: { [key: string]: string } = {};
|
let i = 0;
|
||||||
for (const year in groupedTransactions) {
|
categories.forEach((category: string) => {
|
||||||
for (let index = 0; index < groupedTransactions[year].length; index++) {
|
|
||||||
const transaction = groupedTransactions[year][index];
|
|
||||||
if (!categories[transaction.category]) {
|
|
||||||
categories[transaction.category] = getRandomColor();
|
|
||||||
chartData.datasets.push({
|
chartData.datasets.push({
|
||||||
label: transaction.category,
|
label: category,
|
||||||
data: [],
|
data: [],
|
||||||
backgroundColor: categories[transaction.category]
|
backgroundColor: colorScale[i % colorScale.length], // Use a color from the color scale,
|
||||||
|
borderColor: 'black',
|
||||||
|
borderWidth: 2
|
||||||
|
});
|
||||||
|
i++;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log('here');
|
|
||||||
|
|
||||||
// Populate the chart data with the transaction amounts
|
// Populate the chart data with the transaction amounts
|
||||||
for (const year in groupedTransactions) {
|
for (const key of Object.keys(groupedTransactions)) {
|
||||||
chartData.labels.push(year);
|
chartData.labels.push(key);
|
||||||
|
const transactions = groupedTransactions[key];
|
||||||
for (let i = 0; i < chartData.datasets.length; i++) {
|
for (let i = 0; i < chartData.datasets.length; i++) {
|
||||||
chartData.datasets[i].data.push(0);
|
chartData.datasets[i].data.push(0);
|
||||||
}
|
for (const transaction of transactions) {
|
||||||
for (const transaction of groupedTransactions[year]) {
|
if (transaction.category === chartData.datasets[i].label) {
|
||||||
for (let i = 0; i < chartData.datasets.length; i++) {
|
|
||||||
if (chartData.datasets[i].label === transaction.category) {
|
|
||||||
chartData.datasets[i].data[chartData.labels.length - 1] += transaction.amount;
|
chartData.datasets[i].data[chartData.labels.length - 1] += transaction.amount;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return chartData;
|
return chartData;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ type ChartData = {
|
|||||||
datasets: {
|
datasets: {
|
||||||
label: string;
|
label: string;
|
||||||
backgroundColor: string;
|
backgroundColor: string;
|
||||||
|
borderColor: string;
|
||||||
|
borderWidth: number;
|
||||||
data: number[];
|
data: number[];
|
||||||
}[];
|
}[];
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user