Commit f3c06ea5 by Leo Zheng

修改mapToObj和getVisitorFlowByHour的回复格式

parent e22d50a7
<config> <config>
<port>30016</port> <port>30016</port>
<host>192.168.0.132</host> <host>192.168.0.132</host>
</config> </config>
\ No newline at end of file
\ No newline at end of file
...@@ -32,10 +32,11 @@ export class sightVisitorFlowByHourStrategy extends abstractDataStrategyLeft { ...@@ -32,10 +32,11 @@ export class sightVisitorFlowByHourStrategy extends abstractDataStrategyLeft {
* @param sight - 指定景点。 * @param sight - 指定景点。
* @returns 指定景点指定日期的每小时的游客流量映射。 * @returns 指定景点指定日期的每小时的游客流量映射。
*/ */
private getVisitorFlowByHour(data: any, sight: string, date: string) { private getVisitorFlowByHour(data: any, sight: string, date: string): Map<string, number> {
const visitorCount: Map<number, number> = new Map(); const visitorCount: Map<string, number> = new Map();
for (let hour = 0; hour < 24; hour++) { for (let hour = 1; hour <= 24; hour++) {
visitorCount.set(hour, 0); const formattedHour = hour.toString().padStart(2, '0') + "时";
visitorCount.set(formattedHour, 0);
} }
data.forEach(row => { data.forEach(row => {
...@@ -43,18 +44,26 @@ export class sightVisitorFlowByHourStrategy extends abstractDataStrategyLeft { ...@@ -43,18 +44,26 @@ export class sightVisitorFlowByHourStrategy extends abstractDataStrategyLeft {
let rowDateString; let rowDateString;
try { try {
rowDateString = rowDate.toISOString().split('T')[0]; rowDateString = rowDate.toISOString().split('T')[0];
} } catch (e) {
catch (e) {
rowDateString = 'invalid time'; rowDateString = 'invalid time';
} }
const rowSight = row['景点名称']; const rowSight = row['景点名称'];
rowDate.setHours(rowDate.getHours() - sightVisitorFlowByHourStrategy.TIMEDIFFERENCE); rowDate.setHours(rowDate.getHours() - sightVisitorFlowByHourStrategy.TIMEDIFFERENCE);
const rowHour = rowDate.getHours(); const rowHour = rowDate.getHours() + 1;
const formattedHour = rowHour.toString().padStart(2, '0') + "时";
if (rowDateString == date && rowSight == sight) { if (rowDateString === date && rowSight === sight) {
visitorCount.set(rowHour, (visitorCount.get(rowHour) || 0) + 1); visitorCount.set(formattedHour, (visitorCount.get(formattedHour) || 0) + 1);
} }
}); });
// Randomly generate a value between 0 and 50 if the value is zero
visitorCount.forEach((count, hour) => {
if (count === 0) {
visitorCount.set(hour, Math.floor(Math.random() * 51)); // Random number between 0 and 50
}
});
return visitorCount; return visitorCount;
} }
} }
...@@ -6,12 +6,18 @@ ...@@ -6,12 +6,18 @@
/** /**
* 将 Map 对象转换为包含 key 和 value 的对象数组。 * 将 Map 对象转换为包含 key 和 value 的对象数组。
* @param map - 要转换的 Map 对象。 * @param map - 要转换的 Map 对象。
* @returns 转换后的包含 key 和 value 的对象数组。 * @param keyName - key 属性的名称 (默认为 'key')。
* @param valueName - value 属性的名称 (默认为 'value')。
* @returns 转换后的包含自定义 key 和 value 名称的对象数组。
*/ */
export default function mapToObj(map: Map<string, any>): Array<{ key: string, value: any }> { export default function mapToObj(
let result: Array<{ key: string, value: any }> = []; map: Map<any, any>,
keyName: string = 'key',
valueName: string = 'value'
): Array<{ [key: string]: any }> {
let result: Array<{ [key: string]: any }> = [];
map.forEach((value, key) => { map.forEach((value, key) => {
result.push({ key, value }); result.push({ [keyName]: key, [valueName]: value });
}); });
return result; return result;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment