Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
M
monitor-draw-image
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
monitor-draw-image
Commits
52866af4
提交
52866af4
authored
3月 21, 2023
作者:
Matrix
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
时间区域更新
上级
1109ad39
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
58 行增加
和
24 行删除
+58
-24
ChartApp.java
src/main/java/com/example/pt/ChartApp.java
+58
-24
没有找到文件。
src/main/java/com/example/pt/ChartApp.java
浏览文件 @
52866af4
...
...
@@ -3,9 +3,7 @@ package com.example.pt;
import
com.example.pt.collectors.DataRecord
;
import
com.example.pt.collectors.DataRecordService
;
import
javafx.application.Application
;
import
javafx.collections.ListChangeListener
;
import
javafx.geometry.Pos
;
import
javafx.scene.Node
;
import
javafx.scene.Scene
;
import
javafx.scene.chart.CategoryAxis
;
import
javafx.scene.chart.LineChart
;
...
...
@@ -15,13 +13,13 @@ import javafx.scene.control.*;
import
javafx.scene.input.MouseEvent
;
import
javafx.scene.layout.BorderPane
;
import
javafx.scene.layout.HBox
;
import
javafx.scene.layout.StackPane
;
import
javafx.stage.Stage
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
javax.annotation.Resource
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.temporal.ChronoUnit
;
...
...
@@ -63,10 +61,8 @@ public class ChartApp extends Application {
@Override
public
void
start
(
Stage
primaryStage
)
{
// 获取初始数据
List
<
DataRecord
>
dataRecords
=
dataRecordService
.
list
();
// 过滤 rid 为 9999 和 606118 的记录
Map
<
Integer
,
List
<
DataRecord
>>
ridToDataRecordsMap
=
dataRecords
.
stream
().
filter
(
record
->
record
.
getRid
()
==
RID_FENG_GE
||
record
.
getRid
()
==
RID_DA_SI_MA
).
collect
(
Collectors
.
groupingBy
(
DataRecord:
:
getRid
));
List
<
DataRecord
>
initialDataRecords
=
dataRecordService
.
list
();
Map
<
Integer
,
List
<
DataRecord
>>
initialRidToDataRecordsMap
=
initialDataRecords
.
stream
().
filter
(
record
->
record
.
getRid
()
==
RID_FENG_GE
||
record
.
getRid
()
==
RID_DA_SI_MA
).
collect
(
Collectors
.
groupingBy
(
DataRecord:
:
getRid
));
// 创建折线图
CategoryAxis
xAxis
=
new
CategoryAxis
();
...
...
@@ -83,10 +79,27 @@ public class ChartApp extends Application {
intervalComboBox
.
getItems
().
addAll
(
1
,
5
,
10
,
30
,
60
);
intervalComboBox
.
getSelectionModel
().
selectFirst
();
// 添加日期选择器
DatePicker
startDatePicker
=
new
DatePicker
(
LocalDate
.
now
().
minusDays
(
7
));
DatePicker
endDatePicker
=
new
DatePicker
(
LocalDate
.
now
());
// 添加时段选择器
// TextField timeRangeField = new TextField("00:00-23:59");
// 添加时间段选择器
ComboBox
<
String
>
timeRangeComboBox
=
new
ComboBox
<>();
timeRangeComboBox
.
getItems
().
addAll
(
"全天"
,
"上午"
,
"下午"
,
"晚上"
);
timeRangeComboBox
.
getSelectionModel
().
selectFirst
();
// 添加更新按钮
Button
updateButton
=
new
Button
(
"更新"
);
updateButton
.
setOnAction
(
event
->
updateChartData
(
ridToDataRecordsMap
,
intervalComboBox
.
getValue
()));
updateButton
.
setOnAction
(
event
->
{
LocalDate
startDate
=
startDatePicker
.
getValue
();
LocalDate
endDate
=
endDatePicker
.
getValue
();
updateChartData
(
initialRidToDataRecordsMap
,
intervalComboBox
.
getValue
(),
startDate
,
endDate
,
timeRangeComboBox
.
getValue
());
});
HBox
hbox
=
new
HBox
(
10
,
intervalComboBox
,
updateButton
);
HBox
hbox
=
new
HBox
(
10
,
intervalComboBox
,
startDatePicker
,
endDatePicker
,
timeRangeComboBox
,
updateButton
);
hbox
.
setAlignment
(
Pos
.
CENTER
);
// 设置场景并显示图表
...
...
@@ -98,10 +111,15 @@ public class ChartApp extends Application {
primaryStage
.
show
();
// 初始化图表数据
updateChartData
(
ridToDataRecordsMap
,
intervalComboBox
.
getValue
());
LocalDate
startDate
=
startDatePicker
.
getValue
();
LocalDate
endDate
=
endDatePicker
.
getValue
();
String
timeRange
=
timeRangeComboBox
.
getValue
();
updateChartData
(
initialRidToDataRecordsMap
,
intervalComboBox
.
getValue
(),
startDate
,
endDate
,
timeRange
);
}
private
void
updateChartData
(
Map
<
Integer
,
List
<
DataRecord
>>
ridToDataRecordsMap
,
int
intervalInMinutes
)
{
private
void
updateChartData
(
Map
<
Integer
,
List
<
DataRecord
>>
ridToDataRecordsMap
,
int
intervalInMinutes
,
LocalDate
startDate
,
LocalDate
endDate
,
String
timeRange
)
{
lineChart
.
getData
().
clear
();
for
(
Map
.
Entry
<
Integer
,
List
<
DataRecord
>>
entry
:
ridToDataRecordsMap
.
entrySet
())
{
...
...
@@ -112,13 +130,16 @@ public class ChartApp extends Application {
List
<
DataRecord
>
sortedRecords
=
new
ArrayList
<>(
records
);
sortedRecords
.
sort
(
Comparator
.
comparing
(
DataRecord:
:
getTimestamp
));
// 根据日期和时段进行筛选
List
<
DataRecord
>
filteredRecords
=
filterRecordsByDateAndTimeRange
(
sortedRecords
,
startDate
,
endDate
,
timeRange
);
// 计算票数增长量
List
<
DataRecord
>
aggregatedData
=
new
ArrayList
<>();
DataRecord
previousRecord
=
null
;
LocalDateTime
currentIntervalStart
=
sort
edRecords
.
get
(
0
).
getTimestamp
();
int
maxScInCurrentInterval
=
sort
edRecords
.
get
(
0
).
getSc
();
LocalDateTime
currentIntervalStart
=
filter
edRecords
.
get
(
0
).
getTimestamp
();
int
maxScInCurrentInterval
=
filter
edRecords
.
get
(
0
).
getSc
();
for
(
DataRecord
record
:
sort
edRecords
)
{
for
(
DataRecord
record
:
filter
edRecords
)
{
if
(
record
.
getTimestamp
().
isAfter
(
currentIntervalStart
.
plus
(
intervalInMinutes
,
ChronoUnit
.
MINUTES
)))
{
// 当前记录超出当前时间区间,将之前的数据添加到折线图
int
scGrowth
=
previousRecord
==
null
?
maxScInCurrentInterval
:
maxScInCurrentInterval
-
previousRecord
.
getSc
();
...
...
@@ -160,27 +181,40 @@ public class ChartApp extends Application {
});
}
});
// dataPoint.nodeProperty().addListener((observable, oldValue, newValue) -> {
// if (newValue != null) {
// StackPane stackPane = (StackPane) newValue;
// Label label = new Label(String.valueOf(dataPoint.getYValue()));
// stackPane.getChildren().add(label);
// }
// });
}
else
{
isFirstDataPoint
=
false
;
}
}
lineChart
.
getData
().
add
(
series
);
}
}
private
List
<
DataRecord
>
filterRecordsByDateAndTimeRange
(
List
<
DataRecord
>
records
,
LocalDate
startDate
,
LocalDate
endDate
,
String
timeRange
)
{
return
records
.
stream
()
.
filter
(
record
->
{
if
(
startDate
!=
null
&&
endDate
!=
null
)
{
return
!
record
.
getTimestamp
().
toLocalDate
().
isBefore
(
startDate
)
&&
!
record
.
getTimestamp
().
toLocalDate
().
isAfter
(
endDate
);
}
return
true
;
})
.
filter
(
record
->
{
switch
(
timeRange
)
{
case
"上午"
:
return
record
.
getTimestamp
().
toLocalTime
().
getHour
()
>=
0
&&
record
.
getTimestamp
().
toLocalTime
().
getHour
()
<
12
;
case
"下午"
:
return
record
.
getTimestamp
().
toLocalTime
().
getHour
()
>=
12
&&
record
.
getTimestamp
().
toLocalTime
().
getHour
()
<
18
;
case
"晚上"
:
return
record
.
getTimestamp
().
toLocalTime
().
getHour
()
>=
18
&&
record
.
getTimestamp
().
toLocalTime
().
getHour
()
<
24
;
default
:
return
true
;
}
})
.
collect
(
Collectors
.
toList
());
}
@Override
public
void
stop
()
{
springContext
.
close
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论