使用Jfreechart 在applet中画动态曲线图

来源:互联网 发布:故宫淘宝和故宫文创 编辑:程序博客网 时间:2024/06/05 18:41
要在Applet中动态显示处理器利用率的曲线图。下面这个例子就是使用Jfreechart在Applet中动态显示曲线图的例子:
在这个例子中使用了两条曲线,请看效果图:

cpu2.gif

程序很简单,只是简单的将Jfreechart在Application上的曲线图的Demo转到Applet的上,代码如下:

package com.sundy.applet;

/**
 * 
 *  TODO 使用Jfreechart 在applet中画动态曲线图
 *
 *  @author 刘正伟(sundy)
 *  @see mailto:sundy26@126.com
 *  @version 1.0
 *  @since 2005-6-22
 * 
 *  修改记录:
 *  日期              修改人                 描述
 *  -----------------------------------------------------------------------
 
*/


/**
 * @author sundy
 *
 
*/


import java.awt.
*;
import java.awt.
event.ActionEvent;
import java.awt.
event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;

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.ChartFactory;
import org.jfree.chart.axis.ValueAxis;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.
*;

public class CpuUsage extends JApplet {
    
private static final long serialVersionUID = 3257566209007171634L;

    
private double lastValue1;

    
private double lastValue2;

    
class DataGenerator extends Timer implements ActionListener {
        
private static final long serialVersionUID = 3977867288743720504L;

        
public void actionPerformed(ActionEvent actionevent) {

            
double d1 = 0.90000000000000002D + 0.20000000000000001D * Math
                    .random();
            lastValue1 
= lastValue1 * d1;

            
double d2 = 0.90000000000000002D + 0.20000000000000001D * Math
                    .random();
            lastValue2 
= lastValue2 * d2;

            addTotalObservation(lastValue1);
            addFreeObservation(lastValue2);
        }


        DataGenerator() 
{
            super(
100null);
            addActionListener(
this);
        }

    }


    
public CpuUsage() {
         lastValue1 
= 100D;
         lastValue2 
= 100D;

        total 
= new TimeSeries("Cpu00", org.jfree.data.time.Millisecond.class);
        
//total.setHistoryCount(30000);
        free = new TimeSeries("Cpu01", org.jfree.data.time.Millisecond.class);
        
//free.setHistoryCount(30000);

        TimeSeriesCollection timeseriescollection 
= new TimeSeriesCollection();
        timeseriescollection.addSeries(total);
        timeseriescollection.addSeries(free);
        BasicStroke basicstroke 
= new BasicStroke(2.0F02);

        DateAxis horizontaldateaxis 
= new DateAxis("Time");
        NumberAxis verticalnumberaxis 
= new NumberAxis("Value");

        XYLineAndShapeRenderer xylineandshaperenderer 
= new XYLineAndShapeRenderer(
                
truefalse);

        xylineandshaperenderer.setSeriesPaint(
0, Color.red);
        xylineandshaperenderer.setSeriesPaint(
1, Color.green);
        xylineandshaperenderer.setStroke(
new BasicStroke(3F, 02));

        JFreeChart jfreechart 
= ChartFactory.createTimeSeriesChart(
                
"处理器利用率""Time""Value", timeseriescollection,
                
truetruefalse);

        XYPlot xyplot 
= jfreechart.getXYPlot();
        ValueAxis valueaxis 
= xyplot.getDomainAxis();
        valueaxis.setAutoRange(
true);
        valueaxis.setFixedAutoRange(60000D);
        valueaxis 
= xyplot.getRangeAxis();
        valueaxis.setRange(
0.0D, 200D);

        ChartPanel chartpanel 
= new ChartPanel(jfreechart);
        chartpanel.setPopupMenu(
null);
        getContentPane().add(chartpanel);
        (
new DataGenerator()).start();
    }


    
private void addTotalObservation(double d) {

        total.add(
new Millisecond(), d);
    }


    
private void addFreeObservation(double d) {
        free.add(
new Millisecond(), d);
    }


    
private TimeSeries total;

    
private TimeSeries free;

}

posted on 2005-08-12 17:54 sundy 阅读(4847) 评论(15)  编辑  收藏 所属分类: Java

评论

 

请问如何设置动态曲线在X轴的起始位置?
一起学习jfreechart
+ QQ:363816964  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2005-11-22 16:35 同一种调调

请问JFreeChar包放在哪里?那个文件夹下  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2005-11-22 16:43 sundy


将BarCpuUsage放到com/sundy/applet目录下,然后试一试这个:

<html>
<body>
<APPLET ARCHIVE="jfreechart-1.0.0-rc1.jar,
jcommon-1.0.0-rc1.jar" CODE="com.sundy.applet.BarCpuUsage" codebase="."
width=640 height=260 ALT="You should see an applet, not this text.">
</APPLET>
</body>
</html>

-----邮件原件-----
发件人: Harvey Huang [mailto:Harvey.Huang@sanmina-sci.com]
发送时间: 2005年10月13日 13:52
收件人: liuzhw@langchao.com
主题: [SPAM?] Re: 答复: 你好!向你请教一个问题.
重要性: 低

你好!
真不好意思,再次麻烦你.
我想我的环境没问题,我能正常显示JFREECHART网站上的Applet.
我的包名为:com.sundy.applet

html 我放在D:/

<html>
<body>
<APPLET ARCHIVE="jfreechart-1.0.0-rc1.jar,
jcommon-1.0.0-rc1.jar" CODE="BarCpuUsage.class" codebase="com/sundy/applet"
width=640 height=260 ALT="You should see an applet, not this text.">
</APPLET>
</body>
</html>


CLASS文件我放在D:/com/sundy/applet下
BarCpuUsage.class

报错:
java.lang.NoClassDefFoundError: BarCpuUsage (wrong name: com/sundy/applet/BarCpuUsage)

有空帮忙看一下.谢谢!

黄晓华

>>> sundy<sundy@langchao.com> 10/13/05 8:42 am >>>

从报的错误来看(java.lang.NoClassDefFoundError: BarCpuUsage (wrong name:),很
显然是applet没有找到BarCpuUsage类,如果BarCpuUsage的包名是
com.sundy.applet.BarCpuUsage,那么BarCpuUsage.class文件应该放在
com/sundy/applet/BarCpuUsage目录下.

你仔细检查一下在Applet使用class文件的路径看有没有错误.

-----邮件原件-----
发件人: Harvey Huang [mailto:Harvey.Huang@sanmina-sci.com]
发送时间: 2005年10月12日 22:54
收件人: sundy26@126.com
主题: 你好!向你请教一个问题.

刘正伟:
你好!
www.cnweblog.com
我有幸读了你一篇关于jfreechart的文章,我按照你的例子,能生成class文件但是放在
IE浏览器运行时,老是报错.
所以冒泡向你请教一下.
我的HTML

<html><body>
<APPLET ARCHIVE="jfreechart-1.0.0-rc1.jar,
jcommon-1.0.0-rc1.jar" CODE="BarCpuUsage.class"
width=640 height=260 ALT="You should see an applet, not this text."> </APPLET> </body></html>

但是老是报错:

java.lang.NoClassDefFoundError: BarCpuUsage (wrong name:
com/sundy/applet/BarCpuUsage)

我是JAVA的初学者,从事IT,不过我侧重于网络和应用方面,所以还望不吝指教,在此先谢
过!!

_____________________________________________________________________________
  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-03-02 10:49 hjx

楼主,请问怎样修改时间的刷新频率,比如说我要让它五秒钟刷一次什么办  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-03-02 11:09 sundy

使用定时器,定时重绘Jfreechart就行了。  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-03-02 11:11 sundy

这里用的是:
super(100, null);  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-03-21 22:46 马旭

楼主,如果我想让JFreeChart放在本地,applet执行的时候调用本地的库,应该如何设置.由于我正在做一个用在嵌入式系统上的applet,所以服务器端没有足够的空间来放库文件.我在本地能用appletviewer -J-classpath -J%classpath% mainhome.htm来实现正确访问,但一放到服务器上用浏览器查看就不行了,有什么办法没有.谢谢  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-03-22 08:45 sundy

@马旭 sorry,放在本地我也没有试过,应该是先在客户端将jfreechart相关的jar文件拷贝大IE的临时目录中吧?我不确定,不过我看有的软件也是类似的做的,先在客户端安装一些jar文件,然后就可以访问了,你可以查一查相关的资料,应该可以实现。
  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-05-08 16:25 ZCL

LZ,按照你这种方法的话,内存没多久就耗光光了  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-07-10 10:32 aquan

你好,我想用jfreechart来制作K线图,请问应该怎么做?我是初学者很多都不懂,谢谢各位了,我的邮箱是aquan112@gmail.com QQ:308241   回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2006-12-06 17:33 Oscar

你好,请教一下 我现在需要实时的读取数据库绘制动态性能曲线,而applet里面又不能直接调用外包的javabean连接数据库 直接在applet里面class.forname又存在安全隐患 请教有什么好的解决方案啊! 方便的话 谢谢mail一下 交流一下 gzq426@sohu.com  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2007-10-13 10:44 bcoffee

请问不用applet行吗,直接在页面上显示  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2007-10-18 10:45 longlong

你的类名是CpuUsage,BarCpuUsage是从何而来???  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2007-10-20 11:21 longlong

IE能把动态的应用程序直接嵌入到jsp里么  回复  更多评论   

# re: 使用Jfreechart 在applet中画动态曲线图 2007-10-24 11:55 gw

我在自己的tomcat6.0服务器上运行这个applet提示错误 :
java.lang.NoClassDefFoundError: org/jfree/data/time/RegularTimePeriod
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

但是,我在本地用appletviewer运行程序是正常的,而且我已经将jfreechart1.0.6.jar等几个文件 复制到了tomcat下的webapps/ROOT/WEB-INF/lib里了。我的html文件如下:
<h1>Sample Applet <h1>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
ARCHIVE="jfreechart-1.0.6.jar, jcommon-1.0.10.jar"

WIDTH = 640 HEIGHT = 800 >
<PARAM NAME = CODE VALUE = abc.class >
<param name = "type" value = "application/x-java-applet;version=1.6">
<param name = "scriptable" value = "false">

</object>

<!--

<!--"END_CONVERTED_APPLET"-->
  回复  更多评论   

td { font-size: 12px }.commentTextBox { font-family : Verdana; font-size: 13px; }.userData { BEHAVIOR: url(#default#userdata) } # re: 使用Jfreechart 在applet中画动态曲线图 2005-09-07 15:22 brian
原创粉丝点击