JAVA Swing uses Jfreechart for line drawing

Source: Internet
Author: User

The effect is as follows:

  

Implementation steps:

1. Importing the JAR Package

    

These two jar packages are included in the zip file downloaded from Jfreechart official website

2. Code writing

1 Importorg.jfree.chart.ChartFactory;2 ImportOrg.jfree.chart.ChartPanel;3 ImportOrg.jfree.chart.JFreeChart;4 ImportOrg.jfree.chart.axis.DateAxis;5 ImportOrg.jfree.chart.axis.DateTickUnit;6 ImportOrg.jfree.chart.labels.ItemLabelAnchor;7 Importorg.jfree.chart.labels.ItemLabelPosition;8 ImportOrg.jfree.chart.labels.StandardXYItemLabelGenerator;9 ImportOrg.jfree.chart.plot.XYPlot;Ten ImportOrg.jfree.chart.renderer.xy.XYItemRenderer; One ImportOrg.jfree.chart.renderer.xy.XYLineAndShapeRenderer; A ImportOrg.jfree.chart.title.TextTitle; - ImportOrg.jfree.data.time.Month; - Importorg.jfree.data.time.TimeSeries; the Importorg.jfree.data.time.TimeSeriesCollection; - ImportOrg.jfree.ui.TextAnchor; -  - Importjavax.swing.*; + Importjava.awt.*; - ImportJava.text.SimpleDateFormat; +  A  Public classTimeserieschart { at     PrivateTimeserieschart () { -  -         //A site's traffic statistics -TimeSeries timeSeries1 =NewTimeSeries ("A", Month.class); -         //Add Data -Timeseries1.add (NewMonth (1, 2016), 154); inTimeseries1.add (NewMonth (2, 2016), 256); -Timeseries1.add (NewMonth (3, 2016), 312); toTimeseries1.add (NewMonth (4, 2016), 489); +Timeseries1.add (NewMonth (5, 2016), 563); -Timeseries1.add (NewMonth (6, 2016), 555); theTimeseries1.add (NewMonth (7, 2016), 359); *Timeseries1.add (NewMonth (8, 2016), 291); $Timeseries1.add (NewMonth (9, 2016), 123);Panax NotoginsengTimeseries1.add (NewMonth (10, 2016), 438); -Timeseries1.add (NewMonth (11, 2016), 286); the  +         //A site's traffic statistics ATimeSeries TimeSeries2 =NewTimeSeries ("A", Month.class); the         //Add Data +Timeseries2.add (NewMonth (1, 2016), 124); -Timeseries2.add (NewMonth (2, 2016), 326); $Timeseries2.add (NewMonth (3, 2016), 12); $Timeseries2.add (NewMonth (4, 2016), 567); -Timeseries2.add (NewMonth (5, 2016), 546); -Timeseries2.add (NewMonth (6, 2016), 123); theTimeseries2.add (NewMonth (7, 2016), 222); -Timeseries2.add (NewMonth (8, 2016), 545);WuyiTimeseries2.add (NewMonth (9, 2016), 56); theTimeseries2.add (NewMonth (10, 2016), 543); -Timeseries2.add (NewMonth (11, 2016), 221); Wu  -         //define a collection of time series AboutTimeseriescollection Linedataset =Newtimeseriescollection (); $ linedataset.addseries (timeSeries1); - linedataset.addseries (timeSeries2); -  -  A //Jfreechart chart = Chartfactory.createxystepchart ("Time line graph", "M", "F", Xyseriescollection, Plotorient ation. Horizontal, False, False, false); +Jfreechart chart = Chartfactory.createtimeserieschart ("Time line graph", "M", "F", Linedataset,false,false,false); the         //set the main title -Chart.settitle (NewTexttitle ("A/b website traffic statistics comparison Chart")); $         //set sub-headings theTexttitle subtitle =NewTexttitle ("2016 annual",NewFont ("Arial", Font.Bold, 12)); the Chart.addsubtitle (subtitle); the  theChart.setantialias (true); -  in         //sets the range of the timeline.  theXyplot plot =(Xyplot) Chart.getplot (); theDateaxis Dateaxis =(Dateaxis) Plot.getdomainaxis (); AboutDateaxis.setdateformatoverride (NewSimpleDateFormat ("M")); theDateaxis.settickunit (NewDatetickunit (Datetickunit.month, 1)); the  the         //set whether a curve displays data points +Xylineandshaperenderer Xylinerenderer =(Xylineandshaperenderer) plot.getrenderer (); -Xylinerenderer.setbaseshapesvisible (true); the Bayi         //set the curve to show the values of each data point theXyitemrenderer Xyitem =plot.getrenderer (); theXyitem.setbaseitemlabelsvisible (true); -Xyitem.setbasepositiveitemlabelposition (Newitemlabelposition (Itemlabelanchor.outside12, Textanchor.baseline_center)); -Xyitem.setbaseitemlabelgenerator (Newstandardxyitemlabelgenerator ()); theXyitem.setbaseitemlabelfont (NewFont ("Dialog", Font.Bold, 12)); the Plot.setrenderer (xyitem); the  theJPanel JPanel =NewChartpanel (chart); -  the  theJFrame frame =NewJFrame ("Jfreechart Test"); the Frame.add (jPanel);94Frame.setbounds (0, 0, 800, 600); theFrame.setvisible (true); the     } the 98      Public Static voidMain (string[] args) { AboutTimeserieschart Timeserieschart =NewTimeserieschart (); -     }101}

JAVA Swing uses Jfreechart for line drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.