您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用JFreeChart怎么動態(tài)畫折線圖,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
需要的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);// 標(biāo)記線是否顯示 numberaxis.setTickMarkInsideLength(0);// 外刻度線向內(nèi)長度 numberaxis.setTickMarkOutsideLength(0); // X軸的設(shè)計 NumberAxis x = (NumberAxis) xyplot.getDomainAxis(); x.setAutoRange(true);// 自動設(shè)置數(shù)據(jù)軸數(shù)據(jù)范圍 // 自己設(shè)置橫坐標(biāo)的值 x.setAutoTickUnitSelection(false); x.setTickUnit(new NumberTickUnit(60d)); // 設(shè)置最大的顯示值和最小的顯示值 x.setLowerBound(0); x.setUpperBound(60); // 數(shù)據(jù)軸的數(shù)據(jù)標(biāo)簽:只顯示整數(shù)標(biāo)簽 x.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); x.setAxisLineVisible(true);// X軸豎線是否顯示 x.setTickMarksVisible(false);// 標(biāo)記線是否顯示 RectangleInsets offset = new RectangleInsets(0, 0, 0, 0); xyplot.setAxisOffset(offset);// 坐標(biāo)軸到數(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è)置標(biāo)題字體 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)容,你們對使用JFreeChart怎么動態(tài)畫折線圖有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。