动态 JFreeChart 时间序列
基于这个example,我是现在能够收集数据并将其显示在图表中.我不知道如何将生成图表的代码集成到我的应用程序中.
/** @see http://stackoverflow.com/questions/5048852 */公共类 Atol 扩展 ApplicationFrame {private static final String TITLE =动态系列";private static final String START = "开始";私有静态最终字符串 STOP =停止";私有静态最终浮点数 MINMAX = 100;私有静态最终 int 计数 = 2 * 60;私有静态最终 int FAST = 100;私有静态最终 int SLOW = FAST * 5;私有静态最终随机随机=新随机();私人定时器定时器;公共 Atol(最终字符串标题){超级(标题);最终 DynamicTimeSeriesCollection 数据集= new DynamicTimeSeriesCollection(1, COUNT, new Second());dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));dataset.addSeries(gaussianData(), 0, 高斯数据");JFreeChart 图表 = createChart(dataset);最终的 JButton 运行 = 新的 JButton(STOP);run.addActionListener(new ActionListener() {@覆盖公共无效actionPerformed(ActionEvent e){字符串 cmd = e.getActionCommand();如果(STOP.equals(cmd)){计时器.stop();run.setText(START);} 别的 {计时器.start();运行.setText(停止);}}});最终的 JComboBox 组合 = 新的 JComboBox();combo.addItem("Fast");combo.addItem(慢");组合.addActionListener(新的ActionListener(){@覆盖公共无效actionPerformed(ActionEvent e){if ("Fast".equals(combo.getSelectedItem())) {timer.setDelay(FAST);} 别的 {timer.setDelay(SLOW);}}});this.add(new ChartPanel(chart), BorderLayout.CENTER);JPanel btnPanel = new JPanel(new FlowLayout());btnPanel.add(运行);btnPanel.add(组合);this.add(btnPanel, BorderLayout.SOUTH);系统信息 si = 新系统信息();//Criando uma nova classe de infos do SistemHardwareAbstractionLayer hal = si.getHardware();//Infos de Hardware do sistemaCentralProcessor cpu = hal.getProcessor();//E as informações da cpulong[] oldTricks = cpu.getSystemCpuLoadTicks();计时器 = 新计时器(快速,新 ActionListener(){浮动CPU(){双统计 = cpu.getSystemCpuLoadBetweenTicks(oldTricks);//Convertendo o valor de uso da CPU统计 = 统计 * 100d;双 teste = Math.round(stats * 100.0)/100.0;双 d = 睾丸;浮动 f = (浮动) d;System.out.println(f);返回 f;}浮动[]新数据=新浮动[1];@覆盖公共无效actionPerformed(ActionEvent e){新数据[0] = cpu();数据集.advanceTime();dataset.appendData(newData);}});}私人浮动[] gaussianData() {浮动[] a = 新浮动[计数];for (int i = 0; i < a.length; i++) {a[i] = 2;}返回一个;}私人 JFreeChart createChart(最终 XYDataset 数据集){最终 JFreeChart 结果 = ChartFactory.createTimeSeriesChart(TITLE、hh:mm:ss"、milliVolts"、数据集、真、真、假);最终 XYPlot 图 = result.getXYPlot();ValueAxis domain = plot.getDomainAxis();域.setAutoRange(真);ValueAxis range = plot.getRangeAxis();range.setRange(-MINMAX, MINMAX);返回结果;}公共无效开始(){计时器.start();}公共静态无效主要(最终字符串[]参数){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){Atol 演示 = new Atol(TITLE);演示.pack();demo.setVisible(true);演示.start();}});}
}
到我的 JFrame
代码(在另一个类中):
私有 void kButton3ActionPerformed(java.awt.event.ActionEvent evt) {//当我点击那个按钮时Atol 演示 = 新 Atol ("");chartCPU.add(new ChartPanel(demo));//这就是图表需要出现并重新调整到它的大小的JPanel}
如果 JFreeChart
在一个构造函数中,我如何在 Atol 类中调用图表(JFreeChart 图表)?我没有得到!如何让它出现在我的 chartCPU
JPanel
中?(这是一个名为TelaLogin
的JFrame
类,与Atol
类不同.
您的原始问题询问如何捕获 CPU 数据并将其显示在图表中.由于相关方法具有低延迟,因此可以在 javax.swing.Timer
的操作侦听器中调用该方法,如图 这里和上面.
具有不可预测延迟的数据访问可能会阻塞 事件调度线程,在这种情况下,您应该改用 SwingWorker
,如此处所示.p>
要将图表集成到您的应用程序中,请将示例的构造函数重构为工厂方法并采用以下方法之一:
CardLayout
,显示在此处.JTabbedPane
,显示在此处.JInternalFrame
,显示在此处和这里.
[EDIT] Based on this example, I am now able to collect data and display it in a chart. I don't know exactly how to integrate that code that generates the chart into my application.
/** @see http://stackoverflow.com/questions/5048852 */
public class Atol extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;
public Atol(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset
= new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
dataset.addSeries(gaussianData(), 0, "Gaussian data");
JFreeChart chart = createChart(dataset);
final JButton run = new JButton(STOP);
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (STOP.equals(cmd)) {
timer.stop();
run.setText(START);
} else {
timer.start();
run.setText(STOP);
}
}
});
final JComboBox combo = new JComboBox();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
} else {
timer.setDelay(SLOW);
}
}
});
this.add(new ChartPanel(chart), BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(run);
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);
SystemInfo si = new SystemInfo(); //Criando uma nova classe de infos do Sistem
HardwareAbstractionLayer hal = si.getHardware(); //Infos de Hardware do sistema
CentralProcessor cpu = hal.getProcessor(); //E as informações da cpu
long[] oldTricks = cpu.getSystemCpuLoadTicks();
timer = new Timer(FAST, new ActionListener() {
float cpu() {
Double stats = cpu.getSystemCpuLoadBetweenTicks(oldTricks);
//Convertendo o valor de uso da CPU
stats = stats * 100d;
double teste = Math.round(stats * 100.0) / 100.0;
double d = teste;
float f = (float) d;
System.out.println(f);
return f;
}
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = cpu();
dataset.advanceTime();
dataset.appendData(newData);
}
});
}
private float[] gaussianData() {
float[] a = new float[COUNT];
for (int i = 0; i < a.length; i++) {
a[i] = 2;
}
return a;
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "hh:mm:ss", "milliVolts", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Atol demo = new Atol(TITLE);
demo.pack();
demo.setVisible(true);
demo.start();
}
});
}
}
To my JFrame
code (which is in another class):
private void kButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// when I click in that button
Atol demo = new Atol ("");
chartCPU.add(new ChartPanel(demo)); //Thats the JPanel the chart need to appear and readjust to it size
}
How can I call the chart (JFreeChart chart) in Atol class if the JFreeChart
is in one constructor? I didn't get!
How can I make it appear in my chartCPU
JPanel
? (which is one JFrame
class called TelaLogin
, not the same as the Atol
one).
Your original question asked how to capture CPU data and display it in a chart. Because the relevant method has low latency, it's possible to invoke the method in the action listener of a javax.swing.Timer
as shown here and above.
Data access with unpredictable latency may block the event dispatch thread, in which case you should use a SwingWorker
instead, as shown here.
To integrate the chart into your application, refactor the example's constructor into a factory method and adopt an approach such as one of these:
CardLayout
, shown here.JTabbedPane
, shown here.JInternalFrame
, shown here and here.
相关文章