您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用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怎么動態畫折線圖有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。