本文實例為大家分享了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(); }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。