This article shares the specific code of JFreeChart dynamic line chart for your reference. The specific content is as follows
Draw once every second and draw again after one minute
The required jar packages are: 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 hundreds = 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("Bold", Font.BOLD, 20)); mChartTheme.setExtraLargeFont(new Font("Zongyi", Font.PLAIN, 15)); mChartTheme.setRegularFont(new Font("Zongyi", Font.PLAIN, 15)); ChartFactory.setChartTheme(mChartTheme); jfreechart.setBorderPaint(new Color(0,204,205)); jfreechart.setBorderVisible(true); XYPlot xyplot = (XYPlot) jfreechart.getPlot(); // Y-axis NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis(); numberaxis.setLowerBound(0); numberaxis.setUpperBound(100); numberaxis.setTickUnit(new NumberTickUnit(100d)); // Only integer values are displayed numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // numberaxis.setAutoRangeIncludesZero(true); numberaxis.setLowerMargin(0); // Under the data axis (left) margin numberaxis.setMinorTickMarksVisible(false); // Whether marking line displays numberaxis.setTickMarkInsideLength(0); // Inward length of outer tick mark numberaxis.setTickMarkOutsideLength(0); // Design of X-axis NumberAxis x = (NumberAxis) xyplot.getDomainAxis(); x.setAutoRange(true); // Automatically set the data range of the data axis// Set the value of the horizontal coordinate by yourself x.setAutoTickUnitSelection(false); x.setTickUnit(new NumberTickUnit(60d)); // Set the maximum display value and the minimum display value x.setLowerBound(0); x.setUpperBound(60); // Data label of the data axis: only display integer label x.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); x.setAxisLineVisible(true); // Whether the X-axis vertical line displays x.setTickMarksVisible(false); // Whether the marking line displays RectangleInsets offset = new RectangleInsets(0, 0, 0, 0); xyplot.setAxisOffset(offset);// The spacing between the axis and the data area xyplot.setBackgroundAlpha(0.0f);// Remove the background color of the bar chart xyplot.setOutlinePaint(null);// Remove the border// ChartPanel chartPanel = new ChartPanel(jfreechart); // chartPanel.restoreAutoDomainBounds();// Reset the X-axis ChartPanel chartPanel = new ChartPanel(jfreechart, true); return chartPanel; } /** * This method is the design of data* * @return */ public static XYDataset createDataset1() { XYSeriesCollection xyseriescollection = new XYSeriesCollection(); xyseriescollection.addSeries(xyCPUseries); return xyseriescollection; } /** * Random generated data*/ public static void dynamicRun() { int i = 0; while (true) { double factor = Math.random()*100; hundreds = (int)factor; jfreechart.setTitle("The size of the CPU is: "+hundroud+"%"); jfreechart.getTitle().setFont(new Font("Microsoft Yahei", 0, 16));//Set the title font 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); // The window is located in the center of the screen frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); dynamicRun(); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.