diff --git a/pom.xml b/pom.xml index 5e73fa7..a49e1fa 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ 5.7.16 1.2.78 5.8.6 + 1.5.3 @@ -120,6 +121,12 @@ oshi-core ${oshi-core.version} + + + org.jfree + jfreechart + ${jfreechart.version} + diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.form b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.form index aa88354..55d816f 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.form +++ b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.form @@ -36,7 +36,7 @@ - + @@ -45,7 +45,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -96,7 +96,7 @@ - + @@ -122,9 +122,18 @@ - + + + + + + + + + + diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java index 3251db4..38b9cd1 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java +++ b/src/main/java/com/luoboduner/moo/info/ui/form/CpuForm.java @@ -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 processorProgressBars; private static List 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);