当前位置: 首页 > 编程笔记 >

JFreeChart动态画折线图的方法

梁成双
2023-03-14
本文向大家介绍JFreeChart动态画折线图的方法,包括了JFreeChart动态画折线图的方法的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了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();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 主要内容:什么是JFreeChart 折线图,JFreeChart 折线图的示例什么是JFreeChart 折线图 折线图是由直线段连接的一系列点。信息通过这些连接线显示。折线图表示数据如何以相同的时间频率变化。 下图显示了 JFreeChart 库中包含的折线图的一些演示版本: JFreeChart 折线图的示例 让我们考虑一个示例数据,它显示了我们网站www.xnip.cn上的流量数据。 日期 每日访客人数 2016-12-19 200 2016-12-20 150 20

  • 本文向大家介绍JFreeChart折线图的生成方法,包括了JFreeChart折线图的生成方法的使用技巧和注意事项,需要的朋友参考一下 JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar chart

  • 本文向大家介绍Android绘制动态折线图,包括了Android绘制动态折线图的使用技巧和注意事项,需要的朋友参考一下 所谓动态折线图,就是折线图能随着手指的滑动进行动态绘制,这里很定会产生动画效果。基于这个效果,这里使用SurfaceView进行制图。 实现步奏如下: (1): 这里新建一个绘图ChartView,继承SurfaceView并实现SurfaceHolder.Callback ,

  • 问题内容: 我有可更新的OHLCChart。我需要在图表上画一条线。 如何执行呢? 问题答案: 如果要在轴上的给定位置绘制垂直或水平线,则可以使用ValueMarker: 如果要绘制水平线,请使用。

  • 我想把文本放在我在折线图中绘制的每个点上。

  • 我试图显示一个图像的直方图,只显示一些颜色。我已经用JFreeChart和createXYLineChart实现了这一点,并通过遍历所有像素来获取所有数据。 为了加快速度,我尝试用“createhistogram”来完成它。我遵循了这个准则。 为了用新值更新图表,我使用了以下两种方法: setHistogram是一种方法,它根据激活的复选框(布尔红色、绿色和蓝色)返回HistogramDatase

  • 基本折线图 <template> <ve-line :data="chartData" :settings="chartSettings"></ve-line> </template> <script> export default { data () { this.chartSettings = {} return { chartData: { columns: ['日期',

  • 实时显示传感器数据。 用法 Your browser does not support the video tag. 案例:数据变化趋势 功能:显示数字改变的规律