JFreeChart動態(tài)畫折線圖的方法
本文實例為大家分享了JFreeChart動態(tài)畫折線圖的具體代碼,供大家參考,具體內(nèi)容如下
每隔一秒畫一次,一分鐘后重新畫
需要的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)); // 只顯示整數(shù)值 numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // numberaxis.setAutoRangeIncludesZero(true); numberaxis.setLowerMargin(0); // 數(shù)據(jù)軸下(左)邊距 numberaxis.setMinorTickMarksVisible(false);// 標記線是否顯示 numberaxis.setTickMarkInsideLength(0);// 外刻度線向內(nèi)長度 numberaxis.setTickMarkOutsideLength(0); // X軸的設(shè)計 NumberAxis x = (NumberAxis) xyplot.getDomainAxis(); x.setAutoRange(true);// 自動設(shè)置數(shù)據(jù)軸數(shù)據(jù)范圍 // 自己設(shè)置橫坐標的值 x.setAutoTickUnitSelection(false); x.setTickUnit(new NumberTickUnit(60d)); // 設(shè)置最大的顯示值和最小的顯示值 x.setLowerBound(0); x.setUpperBound(60); // 數(shù)據(jù)軸的數(shù)據(jù)標簽:只顯示整數(shù)標簽 x.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); x.setAxisLineVisible(true);// X軸豎線是否顯示 x.setTickMarksVisible(false);// 標記線是否顯示 RectangleInsets offset = new RectangleInsets(0, 0, 0, 0); xyplot.setAxisOffset(offset);// 坐標軸到數(shù)據(jù)區(qū)的間距 xyplot.setBackgroundAlpha(0.0f);// 去掉柱狀圖的背景色 xyplot.setOutlinePaint(null);// 去掉邊框 // ChartPanel chartPanel = new ChartPanel(jfreechart); // chartPanel.restoreAutoDomainBounds();//重置X軸 ChartPanel chartPanel = new ChartPanel(jfreechart, true); return chartPanel; } /** * 該方法是數(shù)據(jù)的設(shè)計 * * @return */ public static XYDataset createDataset1() { XYSeriesCollection xyseriescollection = new XYSeriesCollection(); xyseriescollection.addSeries(xyCPUseries); return xyseriescollection; } /** * 隨機生成的數(shù)據(jù) */ 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));//設(shè)置標題字體 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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis 入門之MyBatis環(huán)境搭建(第一篇)
Mybatis的前身叫iBatis,本是apache的一個開源項目, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis。這篇文章主要介紹了Mybatis入門第一篇之MyBaits環(huán)境搭建,需要的朋友參考下2016-12-12如何使用SpringBoot進行優(yōu)雅的數(shù)據(jù)驗證
這篇文章主要介紹了如何使用SpringBoot進行優(yōu)雅的數(shù)據(jù)驗證,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Springboot關(guān)于自定義stater的yml無法提示問題解決方案
這篇文章主要介紹了Springboot關(guān)于自定義stater的yml無法提示問題及解決方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06JAVA中的函數(shù)式接口Function和BiFunction詳解
這篇文章主要介紹了JAVA中的函數(shù)式接口Function和BiFunction詳解,JDK的函數(shù)式接口都加上了@FunctionalInterface注解進行標識,但是無論是否加上該注解只要接口中只有一個抽象方法,都是函數(shù)式接口,需要的朋友可以參考下2024-01-01Java縮略圖生成庫之Thumbnailator應(yīng)用說明
Thumbnailator是一個為Java界面更流暢的縮略圖生成庫,從API提供現(xiàn)有的圖像文件和圖像對象的縮略圖中簡化了縮略過程,兩三行代碼就能夠從現(xiàn)有圖片生成縮略圖,使用起來非常方便,需要的朋友可以了解下2012-12-12