|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { Card, CardHeader, CardContent } from '@mui/material'; |
3 | | -import { |
4 | | - ResponsiveContainer, |
5 | | - AreaChart, |
6 | | - Area, |
7 | | - XAxis, |
8 | | - YAxis, |
9 | | - CartesianGrid, |
10 | | - Tooltip, |
11 | | -} from 'recharts'; |
| 3 | +import * as echarts from 'echarts'; |
12 | 4 | import { useTranslate } from 'react-admin'; |
13 | 5 | import { format, subDays, addDays } from 'date-fns'; |
14 | 6 |
|
@@ -47,70 +39,128 @@ const getRevenuePerDay = (orders: Order[]): TotalByDay[] => { |
47 | 39 | const OrderChart = (props: { orders?: Order[] }) => { |
48 | 40 | const { orders } = props; |
49 | 41 | const translate = useTranslate(); |
50 | | - if (!orders) return null; |
| 42 | + const chartRef = React.useRef<HTMLDivElement>(null); |
| 43 | + const chartInstance = React.useRef<echarts.ECharts | null>(null); |
| 44 | + |
| 45 | + React.useEffect(() => { |
| 46 | + if (!orders) return; |
| 47 | + // Initialize chart |
| 48 | + if (chartRef.current) { |
| 49 | + if (!chartInstance.current) { |
| 50 | + chartInstance.current = echarts.init(chartRef.current); |
| 51 | + } |
| 52 | + |
| 53 | + const revenueData = getRevenuePerDay(orders); |
| 54 | + |
| 55 | + // Configure the chart |
| 56 | + const option = { |
| 57 | + xAxis: { |
| 58 | + type: 'time', |
| 59 | + min: addDays(aMonthAgo, 1).getTime(), |
| 60 | + max: new Date().getTime(), |
| 61 | + axisLabel: { |
| 62 | + formatter: (value: number) => dateFormatter(value), |
| 63 | + }, |
| 64 | + }, |
| 65 | + yAxis: { |
| 66 | + type: 'value', |
| 67 | + axisLabel: { |
| 68 | + formatter: (value: number) => `$${value}`, |
| 69 | + }, |
| 70 | + splitLine: { |
| 71 | + show: true, |
| 72 | + lineStyle: { |
| 73 | + type: [3, 4], |
| 74 | + color: '#aaa', |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + tooltip: { |
| 79 | + trigger: 'axis', |
| 80 | + formatter: (params: any) => { |
| 81 | + const param = params[0]; |
| 82 | + return `${dateFormatter(param.value[0])}: ${new Intl.NumberFormat( |
| 83 | + undefined, |
| 84 | + { |
| 85 | + style: 'currency', |
| 86 | + currency: 'USD', |
| 87 | + } |
| 88 | + ).format(param.value[1])}`; |
| 89 | + }, |
| 90 | + axisPointer: { |
| 91 | + type: 'line', |
| 92 | + lineStyle: { |
| 93 | + type: 'dashed', |
| 94 | + dashArray: [3, 3], |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + grid: { |
| 99 | + left: '0%', |
| 100 | + right: '1%', |
| 101 | + bottom: '0%', |
| 102 | + top: '2%', |
| 103 | + containLabel: true, |
| 104 | + }, |
| 105 | + series: [ |
| 106 | + { |
| 107 | + name: 'Revenue', |
| 108 | + type: 'line', |
| 109 | + smooth: true, |
| 110 | + smoothMonotone: 'x', |
| 111 | + symbol: 'none', |
| 112 | + sampling: 'average', |
| 113 | + areaStyle: { |
| 114 | + color: new echarts.graphic.LinearGradient( |
| 115 | + 0, |
| 116 | + 0, |
| 117 | + 0, |
| 118 | + 1, |
| 119 | + [ |
| 120 | + { |
| 121 | + offset: 0.05, |
| 122 | + color: 'rgba(136, 132, 216, 0.8)', |
| 123 | + }, |
| 124 | + { |
| 125 | + offset: 0.95, |
| 126 | + color: 'rgba(136, 132, 216, 0)', |
| 127 | + }, |
| 128 | + ] |
| 129 | + ), |
| 130 | + }, |
| 131 | + lineStyle: { |
| 132 | + color: '#8884d8', |
| 133 | + width: 2, |
| 134 | + }, |
| 135 | + data: revenueData.map(item => [item.date, item.total]), |
| 136 | + }, |
| 137 | + ], |
| 138 | + }; |
| 139 | + |
| 140 | + // Apply the config |
| 141 | + chartInstance.current.setOption(option); |
| 142 | + } |
| 143 | + |
| 144 | + // Handle resize |
| 145 | + const handleResize = () => { |
| 146 | + chartInstance.current?.resize(); |
| 147 | + }; |
| 148 | + |
| 149 | + window.addEventListener('resize', handleResize); |
| 150 | + |
| 151 | + // Cleanup |
| 152 | + return () => { |
| 153 | + window.removeEventListener('resize', handleResize); |
| 154 | + chartInstance.current?.dispose(); |
| 155 | + chartInstance.current = null; |
| 156 | + }; |
| 157 | + }, [orders]); |
51 | 158 |
|
52 | 159 | return ( |
53 | 160 | <Card> |
54 | 161 | <CardHeader title={translate('pos.dashboard.month_history')} /> |
55 | 162 | <CardContent> |
56 | | - <div style={{ width: '100%', height: 300 }}> |
57 | | - <ResponsiveContainer> |
58 | | - <AreaChart data={getRevenuePerDay(orders)}> |
59 | | - <defs> |
60 | | - <linearGradient |
61 | | - id="colorUv" |
62 | | - x1="0" |
63 | | - y1="0" |
64 | | - x2="0" |
65 | | - y2="1" |
66 | | - > |
67 | | - <stop |
68 | | - offset="5%" |
69 | | - stopColor="#8884d8" |
70 | | - stopOpacity={0.8} |
71 | | - /> |
72 | | - <stop |
73 | | - offset="95%" |
74 | | - stopColor="#8884d8" |
75 | | - stopOpacity={0} |
76 | | - /> |
77 | | - </linearGradient> |
78 | | - </defs> |
79 | | - <XAxis |
80 | | - dataKey="date" |
81 | | - name="Date" |
82 | | - type="number" |
83 | | - scale="time" |
84 | | - domain={[ |
85 | | - addDays(aMonthAgo, 1).getTime(), |
86 | | - new Date().getTime(), |
87 | | - ]} |
88 | | - tickFormatter={dateFormatter} |
89 | | - /> |
90 | | - <YAxis dataKey="total" name="Revenue" unit="$" /> |
91 | | - <CartesianGrid strokeDasharray="3 3" /> |
92 | | - <Tooltip |
93 | | - cursor={{ strokeDasharray: '3 3' }} |
94 | | - formatter={(value: any) => |
95 | | - new Intl.NumberFormat(undefined, { |
96 | | - style: 'currency', |
97 | | - currency: 'USD', |
98 | | - }).format(value) |
99 | | - } |
100 | | - labelFormatter={(label: any) => |
101 | | - dateFormatter(label) |
102 | | - } |
103 | | - /> |
104 | | - <Area |
105 | | - type="monotone" |
106 | | - dataKey="total" |
107 | | - stroke="#8884d8" |
108 | | - strokeWidth={2} |
109 | | - fill="url(#colorUv)" |
110 | | - /> |
111 | | - </AreaChart> |
112 | | - </ResponsiveContainer> |
113 | | - </div> |
| 163 | + <div ref={chartRef} style={{ width: '100%', height: 300 }} /> |
114 | 164 | </CardContent> |
115 | 165 | </Card> |
116 | 166 | ); |
|
0 commit comments