当前位置: 移动技术网 > IT编程>开发语言>Java > JFreeChart动态画折线图的方法

JFreeChart动态画折线图的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了jfreechart动态画折线图的具体代码,供大家参考,具体内容如下 每隔一秒画一次,一分钟后重新画 需要的jar包是:gnujaxp.jar,

本文实例为大家分享了jfreechart动态画折线图的具体代码,供大家参考,具体内容如下

每隔一秒画一次,一分钟后重新画

需要的jar包是:gnujaxp.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar

public class jfreezhexiantest{

 public static xyseries xycpuseries = new xyseries("cpu");

 public static int hundroud = 0;
 public static jfreechart jfreechart = null;

 public jpanel getcpujfreechart(){

  jfreechart = chartfactory.createxylinechart(
    null, null, null, createdataset1(),
    plotorientation.vertical, false, true, false);

  standardcharttheme mcharttheme = new standardcharttheme("cn");
  mcharttheme.setlargefont(new font("黑体", font.bold, 20));
  mcharttheme.setextralargefont(new font("宋体", font.plain, 15));
  mcharttheme.setregularfont(new font("宋体", font.plain, 15));
  chartfactory.setcharttheme(mcharttheme);

  jfreechart.setborderpaint(new color(0,204,205));
  jfreechart.setbordervisible(true);

  xyplot xyplot = (xyplot) jfreechart.getplot();

  // y轴
  numberaxis numberaxis = (numberaxis) xyplot.getrangeaxis();
  numberaxis.setlowerbound(0);
  numberaxis.setupperbound(100);
  numberaxis.settickunit(new numbertickunit(100d));
  // 只显示整数值
  numberaxis.setstandardtickunits(numberaxis.createintegertickunits());
  // numberaxis.setautorangeincludeszero(true);
  numberaxis.setlowermargin(0); // 数据轴下(左)边距 ­
  numberaxis.setminortickmarksvisible(false);// 标记线是否显示
  numberaxis.settickmarkinsidelength(0);// 外刻度线向内长度
  numberaxis.settickmarkoutsidelength(0);

  // x轴的设计
  numberaxis x = (numberaxis) xyplot.getdomainaxis();
  x.setautorange(true);// 自动设置数据轴数据范围
  // 自己设置横坐标的值
  x.setautotickunitselection(false);
  x.settickunit(new numbertickunit(60d));
  // 设置最大的显示值和最小的显示值
  x.setlowerbound(0);
  x.setupperbound(60);
  // 数据轴的数据标签:只显示整数标签
  x.setstandardtickunits(numberaxis.createintegertickunits());
  x.setaxislinevisible(true);// x轴竖线是否显示
  x.settickmarksvisible(false);// 标记线是否显示

  rectangleinsets offset = new rectangleinsets(0, 0, 0, 0);
  xyplot.setaxisoffset(offset);// 坐标轴到数据区的间距
  xyplot.setbackgroundalpha(0.0f);// 去掉柱状图的背景色
  xyplot.setoutlinepaint(null);// 去掉边框

  // chartpanel chartpanel = new chartpanel(jfreechart);
  // chartpanel.restoreautodomainbounds();//重置x轴

  chartpanel chartpanel = new chartpanel(jfreechart, true);

  return chartpanel;
 }

 /**
  * 该方法是数据的设计
  * 
  * @return
  */
 public static xydataset createdataset1() {
  xyseriescollection xyseriescollection = new xyseriescollection();
  xyseriescollection.addseries(xycpuseries);
  return xyseriescollection;
 }

 /**
  * 随机生成的数据
  */
 public static void dynamicrun() {
  int i = 0;
  while (true) {

   double factor = math.random()*100;

   hundroud = (int)factor;
   jfreechart.settitle("cpu的大小是:   "+hundroud+"%");
   jfreechart.gettitle().setfont(new font("微软雅黑", 0, 16));//设置标题字体

   xycpuseries.add(i, factor);

   try {
    thread.currentthread();
    thread.sleep(1000);
   } catch (interruptedexception e) {
    e.printstacktrace();
   }
   i++;
   if (i == 60){
    i=0;
    xycpuseries.delete(0, 59);
    continue;
   }
  }
 }

 public static void main(string[] args) {
  jfreezhexiantest jz = new jfreezhexiantest();
  jframe frame = new jframe();
  frame.setsize(700, 500);
  frame.getcontentpane().add(jz.getcpujfreechart(), borderlayout.center);

  frame.setvisible(true);
  frame.setlocationrelativeto(null); // 窗口居于屏幕正中央
  frame.setdefaultcloseoperation(windowconstants.exit_on_close);
  dynamicrun();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网