Skip to content

Commit 9432302

Browse files
author
fangedShadow
committed
fixing tests issues
1 parent 6848051 commit 9432302

4 files changed

Lines changed: 137 additions & 242 deletions

File tree

src/components/LBDashboard/LbAnalytics/DemandOverTime/PropertyDemandChart.jsx

Lines changed: 11 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import {
1313
Tooltip,
1414
Title as ChartTitle,
1515
} from 'chart.js';
16+
import {
17+
getItemColors,
18+
createChartOptions,
19+
createDatasets,
20+
getChartTitle,
21+
} from '../../../../utils/lbDashboard/chartsUtils';
1622

1723
ChartJS.register(
1824
LineElement,
@@ -25,128 +31,15 @@ ChartJS.register(
2531
ChartDataLabels,
2632
);
2733

28-
const metricLabels = {
29-
pageVisits: 'Page Visits',
30-
numberOfBids: 'Number of Bids',
31-
averageRating: 'Average Rating',
32-
averageBid: 'Average Bid',
33-
finalPrice: 'Final Price/Income',
34-
occupancyRate: 'Occupancy Rate (%)',
35-
averageDuration: 'Average Duration of Stay (days)',
36-
};
37-
38-
const metricCategories = {
39-
pageVisits: 'Demand',
40-
numberOfBids: 'Demand',
41-
averageRating: 'Demand',
42-
averageBid: 'Revenue',
43-
finalPrice: 'Revenue',
44-
occupancyRate: 'Vacancy',
45-
averageDuration: 'Vacancy',
46-
};
47-
48-
function getPropertyColors(properties) {
49-
const colors = [
50-
'#FF6B6B',
51-
'#4ECDC4',
52-
'#FFD93D',
53-
'#1A535C',
54-
'#FF9F1C',
55-
'#2EC4B6',
56-
'#E71D36',
57-
'#3A86FF',
58-
];
59-
const colorMap = {};
60-
let idx = 0;
61-
properties.forEach(p => {
62-
if (!colorMap[p]) {
63-
colorMap[p] = colors[idx % colors.length];
64-
idx++;
65-
}
66-
});
67-
return colorMap;
68-
}
69-
7034
const PropertyDemandChart = ({ data, metric, chartLabel, darkMode }) => {
7135
const months = data.length > 0 ? data[0].data.map(d => d.month) : [];
72-
7336
const propertyNames = data.map(p => p.name);
74-
const propertyColors = getPropertyColors(propertyNames);
75-
76-
const datasets = data.map(property => ({
77-
label: property.name,
78-
data: property.data.map(point => point.value),
79-
borderColor: propertyColors[property.name],
80-
backgroundColor: propertyColors[property.name],
81-
fill: false,
82-
tension: 0.4,
83-
pointRadius: 5,
84-
pointHoverRadius: 7,
85-
}));
86-
87-
const chartData = {
88-
labels: months,
89-
datasets,
90-
};
91-
92-
const options = {
93-
responsive: true,
94-
maintainAspectRatio: false,
95-
plugins: {
96-
legend: {
97-
position: 'bottom',
98-
labels: { font: { size: 13 }, color: darkMode ? '#fff' : '#222' },
99-
},
100-
title: { display: false },
101-
datalabels: {
102-
color: darkMode ? '#fff' : '#333',
103-
font: { weight: 'bold', size: 11 },
104-
align: 'top',
105-
anchor: 'end',
106-
offset: 4,
107-
clip: false,
108-
display: 'auto',
109-
formatter: value => value,
110-
},
111-
tooltip: {
112-
enabled: true,
113-
callbacks: {
114-
label: function(context) {
115-
return `${context.dataset.label}: ${context.parsed.y}`;
116-
},
117-
},
118-
},
119-
},
120-
layout: {
121-
padding: 20,
122-
},
123-
scales: {
124-
x: {
125-
title: { display: true, text: 'Month', color: darkMode ? '#fff' : '#222' },
126-
offset: true,
127-
ticks: {
128-
autoSkip: false,
129-
maxRotation: 45,
130-
minRotation: 30,
131-
font: { size: 12 },
132-
color: darkMode ? '#fff' : '#222',
133-
},
134-
},
135-
y: {
136-
title: {
137-
display: true,
138-
text: metricLabels[metric] || metric,
139-
color: darkMode ? '#fff' : '#222',
140-
},
141-
beginAtZero: true,
142-
ticks: { font: { size: 12 }, color: darkMode ? '#fff' : '#222' },
143-
},
144-
},
145-
};
37+
const propertyColors = getItemColors(propertyNames);
38+
const datasets = createDatasets(data, propertyColors);
14639

147-
const chartTitleText =
148-
chartLabel ||
149-
`${metricCategories[metric] || 'Metric'}: ${metricLabels[metric] || metric} by Property`;
40+
const chartData = { labels: months, datasets };
41+
const options = createChartOptions(metric, darkMode);
42+
const chartTitleText = getChartTitle(chartLabel, metric, 'Property');
15043

15144
return (
15245
<div className={`${styles.chartCard} ${darkMode ? styles.darkChartCard : ''}`}>

src/components/LBDashboard/LbAnalytics/DemandOverTime/VillageDemandChart.jsx

Lines changed: 12 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import {
1313
Tooltip,
1414
Title as ChartTitle,
1515
} from 'chart.js';
16+
import {
17+
getItemColors,
18+
createChartOptions,
19+
createDatasets,
20+
getChartTitle,
21+
} from '../../../../utils/lbDashboard/chartsUtils';
1622

1723
ChartJS.register(
1824
LineElement,
@@ -25,137 +31,19 @@ ChartJS.register(
2531
ChartDataLabels,
2632
);
2733

28-
const metricLabels = {
29-
pageVisits: 'Page Visits',
30-
numberOfBids: 'Number of Bids',
31-
averageRating: 'Average Rating',
32-
averageBid: 'Average Bid',
33-
finalPrice: 'Final Price/Income',
34-
occupancyRate: 'Occupancy Rate (%)',
35-
averageDuration: 'Average Duration of Stay (days)',
36-
};
37-
38-
const metricCategories = {
39-
pageVisits: 'Demand',
40-
numberOfBids: 'Demand',
41-
averageRating: 'Demand',
42-
averageBid: 'Revenue',
43-
finalPrice: 'Revenue',
44-
occupancyRate: 'Vacancy',
45-
averageDuration: 'Vacancy',
46-
};
47-
48-
function getVillageColors(villages) {
49-
const colors = [
50-
'#FF6B6B',
51-
'#4ECDC4',
52-
'#FFD93D',
53-
'#1A535C',
54-
'#FF9F1C',
55-
'#2EC4B6',
56-
'#E71D36',
57-
'#3A86FF',
58-
];
59-
const colorMap = {};
60-
let idx = 0;
61-
villages.forEach(v => {
62-
if (!colorMap[v]) {
63-
colorMap[v] = colors[idx % colors.length];
64-
idx++;
65-
}
66-
});
67-
return colorMap;
68-
}
69-
7034
const VillageDemandChart = ({ data, metric, chartLabel, darkMode }) => {
7135
const months = data.length > 0 ? data[0].data.map(d => d.month) : [];
72-
7336
const villageNames = data.map(v => v.name);
74-
const villageColors = getVillageColors(villageNames);
75-
76-
const datasets = data.map(village => ({
77-
label: village.name,
78-
data: village.data.map(point => point.value),
79-
borderColor: villageColors[village.name],
80-
backgroundColor: villageColors[village.name],
81-
fill: false,
82-
tension: 0.4,
83-
pointRadius: 5,
84-
pointHoverRadius: 7,
85-
}));
86-
87-
const chartData = {
88-
labels: months,
89-
datasets,
90-
};
37+
const villageColors = getItemColors(villageNames);
38+
const datasets = createDatasets(data, villageColors);
9139

92-
const options = {
93-
responsive: true,
94-
maintainAspectRatio: false,
95-
plugins: {
96-
legend: {
97-
position: 'bottom',
98-
labels: { font: { size: 13 }, color: darkMode ? '#fff' : '#222' },
99-
},
100-
title: { display: false },
101-
datalabels: {
102-
color: darkMode ? '#fff' : '#333',
103-
font: { weight: 'bold', size: 11 },
104-
align: 'top',
105-
anchor: 'end',
106-
offset: 4,
107-
clip: false,
108-
display: 'auto',
109-
formatter: value => value,
110-
},
111-
tooltip: {
112-
enabled: true,
113-
callbacks: {
114-
label: function(context) {
115-
return `${context.dataset.label}: ${context.parsed.y}`;
116-
},
117-
},
118-
},
119-
},
120-
layout: {
121-
padding: 20,
122-
},
123-
scales: {
124-
x: {
125-
title: { display: true, text: 'Month', color: darkMode ? '#fff' : '#222' },
126-
offset: true,
127-
ticks: {
128-
autoSkip: false,
129-
maxRotation: 45,
130-
minRotation: 30,
131-
font: { size: 12 },
132-
color: darkMode ? '#fff' : '#222',
133-
},
134-
},
135-
y: {
136-
title: {
137-
display: true,
138-
text: metricLabels[metric] || metric,
139-
color: darkMode ? '#fff' : '#222',
140-
},
141-
beginAtZero: true,
142-
ticks: { font: { size: 12 }, color: darkMode ? '#fff' : '#222' },
143-
},
144-
},
145-
};
146-
147-
const chartTitleText =
148-
chartLabel ||
149-
`${metricCategories[metric] || 'Metric'}: ${metricLabels[metric] || metric} by Village`;
40+
const chartData = { labels: months, datasets };
41+
const options = createChartOptions(metric, darkMode);
42+
const chartTitleText = getChartTitle(chartLabel, metric, 'Village');
15043

15144
return (
15245
<div className={`${styles.chartCard} ${darkMode ? styles.darkChartCard : ''}`}>
153-
<div
154-
className={darkMode ? styles.darkChartTitle : ''}
155-
style={{ fontSize: 22, fontWeight: 600, margin: '16px 0' }}
156-
>
157-
{chartTitleText}
158-
</div>
46+
<div className={darkMode ? styles.darkChartTitle : styles.chartTitle}>{chartTitleText}</div>
15947
<div className={styles.chart} style={{ height: 350 }}>
16048
<Line data={chartData} options={options} plugins={[ChartDataLabels]} />
16149
</div>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const METRIC_LABELS = {
2+
pageVisits: 'Page Visits',
3+
numberOfBids: 'Number of Bids',
4+
averageRating: 'Average Rating',
5+
averageBid: 'Average Bid',
6+
finalPrice: 'Final Price/Income',
7+
occupancyRate: 'Occupancy Rate (%)',
8+
averageDuration: 'Average Duration of Stay (days)',
9+
};
10+
11+
export const METRIC_CATEGORIES = {
12+
pageVisits: 'Demand',
13+
numberOfBids: 'Demand',
14+
averageRating: 'Demand',
15+
averageBid: 'Revenue',
16+
finalPrice: 'Revenue',
17+
occupancyRate: 'Vacancy',
18+
averageDuration: 'Vacancy',
19+
};
20+
21+
export const CHART_COLORS = [
22+
'#FF6B6B',
23+
'#4ECDC4',
24+
'#FFD93D',
25+
'#1A535C',
26+
'#FF9F1C',
27+
'#2EC4B6',
28+
'#E71D36',
29+
'#3A86FF',
30+
];

0 commit comments

Comments
 (0)