JFreeChart动态画折线图的方法

时间:2022-11-05 23:38:24

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

JFreeChart动态画折线图的方法

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ygl19920119/article/details/78182598