-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
110 lines (99 loc) · 3.19 KB
/
Copy pathApp.tsx
File metadata and controls
110 lines (99 loc) · 3.19 KB
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
import React from 'react';
import './App.css';
import 'devextreme/dist/css/dx.fluent.blue.light.css';
import {
BackgroundColor,
Chart,
CommonPaneSettings,
ConstantLine,
Label,
Series,
Tooltip,
ValueAxis,
} from 'devextreme-react/chart';
import { registerGradient } from 'devextreme-react/common/charts';
import { formatNumber } from 'devextreme/localization';
const chartData = [
{ month: 'January', targetSpend: 12000, actualSpend: 9500 },
{ month: 'February', targetSpend: 15500, actualSpend: 16500 },
{ month: 'March', targetSpend: 13500, actualSpend: 12000 },
{ month: 'April', targetSpend: 16000, actualSpend: 14000 },
{ month: 'May', targetSpend: 17000, actualSpend: 18500 },
{ month: 'June', targetSpend: 20000, actualSpend: 17500 },
{ month: 'July', targetSpend: 14500, actualSpend: 11000 },
{ month: 'August', targetSpend: 21000, actualSpend: 22500 },
{ month: 'September', targetSpend: 19000, actualSpend: 18000 },
{ month: 'October', targetSpend: 22000, actualSpend: 25000 },
{ month: 'November', targetSpend: 16000, actualSpend: 14500 },
{ month: 'December', targetSpend: 25000, actualSpend: 27000 },
];
function calculateAverageSpend(): number {
const sum = chartData.reduce((accumulator, { actualSpend }) => accumulator + actualSpend, 0);
return sum / chartData.length;
}
const averageSpend = calculateAverageSpend();
function customizeTooltip(data: { value?: string | number | Date; seriesName?: string }): { text: string } {
const numValue = typeof data.value === 'number' ? data.value : 0;
if (data.seriesName === 'Budget') {
return { text: formatNumber(numValue, 'currency') };
}
const isValueAboveAverage = numValue > calculateAverageSpend();
if (isValueAboveAverage) {
return { text: `${formatNumber(numValue, 'currency')}\n${formatNumber(numValue - averageSpend, 'currency')} above average spending.` };
}
return { text: `${formatNumber(numValue, 'currency')}\n${formatNumber(averageSpend - numValue, 'currency')} below average spending.` };
}
const chartGradient = registerGradient('linear', {
colors: [{
offset: '20%',
color: '#dee4ff',
}, {
offset: '90%',
color: '#ffdeff',
}],
});
function App(): JSX.Element {
return (
<React.Fragment>
<Chart
dataSource={chartData}
title='Target vs Actual Spending 2025'
>
<Series
type='bar'
argumentField='month'
valueField='targetSpend'
name='Budget'
color='#5996ff'
/>
<Series
type='spline'
argumentField='month'
valueField='actualSpend'
name='Amount Spent'
color='#cb4bfa'
/>
<ValueAxis>
<ConstantLine
value={averageSpend}
color='#0000c7'
>
<Label text='Yearly Spend Average' />
</ConstantLine>
</ValueAxis>
<Tooltip
enabled={true}
customizeTooltip={customizeTooltip}
/>
<CommonPaneSettings>
<BackgroundColor
base='#ffffff'
fillId={chartGradient}
>
</BackgroundColor>
</CommonPaneSettings>
</Chart>
</React.Fragment>
);
}
export default App;