注册
 找回密码
 注册
江西广告网
查看: 502|回复: 0
打印 上一主题 下一主题

JFreeChart做题杂谈

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-3-17 08:14:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.time.*; import org.jfree.ui.RectangleInsets; public class MemoryUsageDemo extends JPanel { class DataGenerator extends Timer implements ActionListener { public void actionPerformed(ActionEvent actionevent) { long l = Runtime.getRuntime().freeMemory(); long l1 = Runtime.getRuntime().totalMemory(); addTotalObservation(l1); addFreeObservation(l); } DataGenerator(int i) { super(i, null); addActionListener(this); } } public MemoryUsageDemo(int i) { super(new BorderLayout()); total = new TimeSeries("Total Memory", org.jfree.data.time.Millisecond.class); total.setMaximumItemAge(i); free = new TimeSeries("Free Memory", org.jfree.data.time.Millisecond.class); free.setMaximumItemAge(i); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); timeseriescollection.addSeries(total); timeseriescollection.addSeries(free); DateAxis dateaxis = new DateAxis("Time"); NumberAxis numberaxis = new NumberAxis("Memory"); dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12)); numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12)); dateaxis.setLabelFont(new Font("SansSerif", 0, 14)); numberaxis.setLabelFont(new Font("SansSerif", 0, 14)); XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(true, false); xylineandshaperenderer.setSeriesPaint(0, Color.red); xylineandshaperenderer.setSeriesPaint(1, Color.green); xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F, 0, 2)); xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(3F, 0, 2)); XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, xylineandshaperenderer); xyplot.setBackgroundPaint(Color.lightGray); xyplot.setDomainGridlinePaint(Color.white); xyplot.setRangeGridlinePaint(Color.white); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); dateaxis.setAutoRange(true); dateaxis.setLowerMargin(0.0D); dateaxis.setUpperMargin(0.0D); dateaxis.setTickLabelsVisible(true); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); JFreeChart jfreechart = new JFreeChart("JVM Memory Usage", new Font("SansSerif", 1, 24), xyplot, true); jfreechart.setBackgroundPaint(Color.white); ChartPanel chartpanel = new ChartPanel(jfreechart, true); chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(Color.black))); add(chartpanel); } private void addTotalObservation(double d) { total.add(new Millisecond(), d); } private void addFreeObservation(double d) { free.add(new Millisecond(), d); } public static void main(String args[]) { JFrame jframe = new JFrame("Memory Usage Demo"); MemoryUsageDemo memoryusagedemo = new MemoryUsageDemo(30000); jframe.getContentPane().add(memoryusagedemo, "Center"); jframe.setBounds(200, 120, 600, 280); jframe.setVisible(true); (memoryusagedemo. new DataGenerator(100)).start(); jframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowevent) { System.exit(0); } } ); } private TimeSeries total; private TimeSeries free; } 以上的代码如果运行的话就成为一个很好的时序图了。可以动态显示 出来电脑内存的变化情况。 好好研究一下其代码。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表