add cpu usage chart
This commit is contained in:
parent
eeac1228ad
commit
dd198cdf75
7
pom.xml
7
pom.xml
|
@ -31,6 +31,7 @@
|
|||
<hutool-all.version>5.7.16</hutool-all.version>
|
||||
<fastjson.version>1.2.78</fastjson.version>
|
||||
<oshi-core.version>5.8.6</oshi-core.version>
|
||||
<jfreechart.version>1.5.3</jfreechart.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
@ -120,6 +121,12 @@
|
|||
<artifactId>oshi-core</artifactId>
|
||||
<version>${oshi-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jfree</groupId>
|
||||
<artifactId>jfreechart</artifactId>
|
||||
<version>${jfreechart.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="a9c92" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="a9c92" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints/>
|
||||
<properties/>
|
||||
|
@ -45,7 +45,7 @@
|
|||
<grid id="3ad43" binding="scuPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
|
@ -69,7 +69,7 @@
|
|||
<grid id="7a05a" binding="pcuPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
|
@ -96,7 +96,7 @@
|
|||
<grid id="5e92c" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="10" left="10" bottom="10" right="10"/>
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
|
@ -122,9 +122,18 @@
|
|||
</grid>
|
||||
<vspacer id="a377f">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<grid id="c1f1" binding="chartPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
|
|
@ -9,6 +9,11 @@ import com.luoboduner.moo.info.App;
|
|||
import com.luoboduner.moo.info.ui.Style;
|
||||
import com.luoboduner.moo.info.util.ScrollUtil;
|
||||
import lombok.Getter;
|
||||
import org.jfree.chart.ChartFactory;
|
||||
import org.jfree.chart.ChartPanel;
|
||||
import org.jfree.chart.JFreeChart;
|
||||
import org.jfree.data.time.DynamicTimeSeriesCollection;
|
||||
import org.jfree.data.time.Second;
|
||||
import oshi.hardware.CentralProcessor;
|
||||
import oshi.hardware.Sensors;
|
||||
|
||||
|
@ -17,8 +22,11 @@ import java.awt.*;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
@ -53,6 +61,7 @@ public class CpuForm {
|
|||
private JLabel voltageLabel;
|
||||
private JLabel fanSpeedsLabel;
|
||||
private JSplitPane splitPane;
|
||||
private JPanel chartPanel;
|
||||
|
||||
private static final Log logger = LogFactory.get();
|
||||
|
||||
|
@ -65,6 +74,8 @@ public class CpuForm {
|
|||
private static List<JProgressBar> processorProgressBars;
|
||||
private static List<JTextField> processorTextFields;
|
||||
|
||||
private static DynamicTimeSeriesCollection sysData;
|
||||
|
||||
public static CpuForm getInstance() {
|
||||
if (cpuForm == null) {
|
||||
cpuForm = new CpuForm();
|
||||
|
@ -143,6 +154,27 @@ public class CpuForm {
|
|||
processorTextFields.add(textField);
|
||||
}
|
||||
|
||||
// Codes bellow are copied from oshi demo.
|
||||
Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
|
||||
sysData = new DynamicTimeSeriesCollection(1, 60, new Second());
|
||||
sysData.setTimeBase(new Second(date));
|
||||
sysData.addSeries(floatArrayPercent(cpuData(cpu)), 0, "All cpus");
|
||||
|
||||
JFreeChart systemCpu = ChartFactory.createTimeSeriesChart(null, null, null, sysData, false,
|
||||
true, false);
|
||||
|
||||
systemCpu.setBackgroundPaint(cpuForm.getChartPanel().getBackground());
|
||||
systemCpu.getXYPlot().setBackgroundPaint(cpuForm.getScuProgressBar().getBackground());
|
||||
systemCpu.getXYPlot().setOutlinePaint(cpuForm.getScuProgressBar().getBackground());
|
||||
systemCpu.getXYPlot().getRenderer().setSeriesPaint(0, cpuForm.getScuProgressBar().getForeground());
|
||||
systemCpu.getXYPlot().getDomainAxis().setVisible(false);
|
||||
systemCpu.getXYPlot().getRangeAxis().setVisible(false);
|
||||
|
||||
cpuForm.getChartPanel().setPreferredSize(new Dimension(-1, 100));
|
||||
cpuForm.getChartPanel().setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
cpuForm.getChartPanel().add(new ChartPanel(systemCpu), new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void initCpuInfo() {
|
||||
|
@ -200,6 +232,9 @@ public class CpuForm {
|
|||
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
|
||||
long totalCpu = Math.max(user + nice + cSys + idle + ioWait + irq + softIrq + steal, 0);
|
||||
|
||||
sysData.advanceTime();
|
||||
sysData.appendData(floatArrayPercent(cpuData(processor)));
|
||||
|
||||
prevTicks = ticks;
|
||||
|
||||
double free = Double.parseDouble(format.format(idle <= 0 ? 0 : (100d * idle / totalCpu)));
|
||||
|
@ -291,6 +326,32 @@ public class CpuForm {
|
|||
cpuForm.getFanSpeedsLabel().setText(Arrays.toString(sensors.getFanSpeeds()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Codes are copied from oshi demo.
|
||||
*
|
||||
* @param d
|
||||
* @return
|
||||
*/
|
||||
private static float[] floatArrayPercent(double d) {
|
||||
float[] f = new float[1];
|
||||
f[0] = (float) (100d * d);
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Codes are copied from oshi demo.
|
||||
*
|
||||
* @param proc
|
||||
* @return
|
||||
*/
|
||||
private static double cpuData(CentralProcessor proc) {
|
||||
if (prevTicks == null) {
|
||||
prevTicks = proc.getSystemCpuLoadTicks();
|
||||
}
|
||||
double d = proc.getSystemCpuLoadBetweenTicks(prevTicks);
|
||||
return d;
|
||||
}
|
||||
|
||||
{
|
||||
// GUI initializer generated by IntelliJ IDEA GUI Designer
|
||||
// >>> IMPORTANT!! <<<
|
||||
|
@ -319,11 +380,11 @@ public class CpuForm {
|
|||
scrollPaneLeft = new JScrollPane();
|
||||
panel2.add(scrollPaneLeft, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
|
||||
final JPanel panel3 = new JPanel();
|
||||
panel3.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
panel3.setLayout(new GridLayoutManager(5, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
scrollPaneLeft.setViewportView(panel3);
|
||||
scuPanel = new JPanel();
|
||||
scuPanel.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
|
||||
panel3.add(scuPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
panel3.add(scuPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
scuTitleLabel = new JLabel();
|
||||
scuTitleLabel.setText("System CPU Usage");
|
||||
scuPanel.add(scuTitleLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
|
||||
|
@ -331,7 +392,7 @@ public class CpuForm {
|
|||
scuPanel.add(scuProgressBar, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
|
||||
pcuPanel = new JPanel();
|
||||
pcuPanel.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
|
||||
panel3.add(pcuPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
panel3.add(pcuPanel, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
pcuTitleLabel = new JLabel();
|
||||
pcuTitleLabel.setText("Processor CPU Usage");
|
||||
pcuPanel.add(pcuTitleLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
|
||||
|
@ -340,7 +401,7 @@ public class CpuForm {
|
|||
pcuPanel.add(pcuProgressBarPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
panel1 = new JPanel();
|
||||
panel1.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
|
||||
panel3.add(panel1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
panel3.add(panel1, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
pcfLabel = new JLabel();
|
||||
pcfLabel.setText("Processor CPU Frequency");
|
||||
panel1.add(pcfLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
|
||||
|
@ -348,7 +409,10 @@ public class CpuForm {
|
|||
pcfPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
panel1.add(pcfPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
final Spacer spacer1 = new Spacer();
|
||||
panel3.add(spacer1, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
|
||||
panel3.add(spacer1, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
|
||||
chartPanel = new JPanel();
|
||||
chartPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
panel3.add(chartPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
final JPanel panel4 = new JPanel();
|
||||
panel4.setLayout(new GridLayoutManager(3, 1, new Insets(10, 0, 10, 10), -1, -1));
|
||||
splitPane.setRightComponent(panel4);
|
||||
|
|
Loading…
Reference in New Issue