??? Jfreechart is a JAVA project on the open source site SourceForge.net, which is used primarily for a wide variety of charts, including: Pie, histogram (regular histogram and stack histogram), line, area, map, mixed, Gantt Chart, some dashboards, and more.
??? Apply Jfreechart to paint requires two jar packages: Jfreechart.jar and Jcommon.jar, directly to the official website to download:
https://sourceforge.net/projects/jfreechart/files/
?? After the download is done, unzip and then import the Jfreechart.jar and Jcommon.jar two packages into the project that needs to draw the table, and import the method:
Right-click the package that contains the project, select Build Path, and then select? Configure? Build Path, click Libraries, then click Add External JARS, add the above two packages can be used
Recently in the write sorting algorithm, suddenly want to compare the actual running time of various sorting algorithms, so draw a chart can be more intuitive comparison.
Code implementation Draw Line chart
class?chart{????????? ? Step 1: Create a Categorydataset object (prepare data)????? Categorydataset?dataset?=?createdataset ();?????? Categorydataset?dataset1?=?createdataset1 ();???? ? Step 2: Build the Jfreechart object according to the dataset, and make the appropriate settings?????? Jfreechart?freechart?=?createchart (dataset);????? Jfreechart?freechart1?=?createchart1 (DataSet1);????????????? Create a Jfreechart object based on Categorydataset???? Public?static? Jfreechart?createchart (categorydataset?categorydateset) {???????????????? ? Create a Jfreechart object: Chartfactory.createlinechart?????????? Jfreechart?jfreechart?=? Chartfactory.createlinechart ("the? Confrontation? Of? Sorts? In? The best? Case ",?//? title????????????????" Sort? By ",????????? Categoryaxislabel (category axis, horizontal, x-axis label)???????????????? " Spent? Time ",?????? ? Valueaxislabel (value axis, vertical axis, y-axis label)???????????????? Categorydateset,?? Dataset?????????????????? Plotorientation.vertical,?false,?//?legend????????????????? False,?????????? ToolTips???????????????? FALSE);???????? URLs???????????????? ? Use Categoryplot to set various parameters.?????????? Categoryplot?plot?=? (Categoryplot) JFreechart.getplot ();?????????????????? ? background color? Transparency?????????? Plot.setbackgroundalpha (0.5f);?????????????????? The front view? Transparency?????????? Plot.setforegroundalpha (1.0f);?????????????????? ? Other settings? reference? Categoryplot class?????????? Lineandshaperenderer?renderer?=? (Lineandshaperenderer) Plot.getrenderer ();?????????? Renderer.setbaseshapesvisible (true);? The series points (that is, data points) are visible?????????? Renderer.setbaselinesvisible (true);? The series points (that is, data points) are visible between the lines?????????? Renderer.setuseseriesoffset (true);? ? Set the offset?????????? Renderer.setbaseitemlabelgenerator (new?) Standardcategoryitemlabelgenerator ());?????????? Renderer.setbaseitemlabelsvisible (true);?????????? Return?jfreechart,??????}???????? Public?static? Categorydataset?createdataset ()? {?????????? String[]?rowkeys?=? {"Sort"};?????????? String[]?colkeys?=? {"Heapsort", "Shellsort", "Bubblesort", "QuickSort", "Radixsort", "Simplesort"};?????????????????? Double[][]?data?=? {{Timechat.btime_heap,timechat.btime_shell,timechat.btime_bubble,timechat.btime_quick,timechat.btime_radix, Timechat.btime_simpLe},};??????????? System.out.println (timechat.btime_heap);???????? Return? Datasetutilities.createcategorydataset (rowkeys,?colkeys,?data);??????}???????? Worst case scenario????? Public?static? Jfreechart?createchart1 (categorydataset?categorydateset) {???????????????? ? Create a Jfreechart object: Chartfactory.createlinechart?????????? Jfreechart?jfreechart?=? Chartfactory.createlinechart ("the? Confrontation? Of? Sorts? In? Worst? Case ",?//? title????????????????" Sort? By ",??????????? Categoryaxislabel (category axis, horizontal, x-axis label)????????????????? " Spent? Time ",??????? Valueaxislabel (value axis, vertical axis, y-axis label)???????????????? Categorydateset,???? //? Dataset???????????????? Plotorientation.vertical,?false,?//?legend????????????????? False,???????????? ToolTips????????????????? FALSE);?????????? //? URLs?????????????????? Categoryplot?plot?=? (Categoryplot) Jfreechart.getplot ();????????? Plot.setbackgroundalpha (0.5f);??????????? Plot.setforegroundalpha (0.5f);???????? Lineandshaperenderer?renderer?=? (Lineandshaperenderer) Plot.getrenderer ();????????? Renderer.setBaseshapesvisible (true);? The series points (that is, data points) are visible????????? Renderer.setbaselinesvisible (true);? The series points (that is, data points) are visible between the lines????????? Renderer.setuseseriesoffset (true);? ? Set the offset????????? Renderer.setbaseitemlabelgenerator (new?) Standardcategoryitemlabelgenerator ());????????? Renderer.setbaseitemlabelsvisible (true);????????? Return?jfreechart,??????}?????????? Public?static? Categorydataset?createdataset1 ()? {????????? String[]?rowkeys?=? {"Sort"};????????? String[]?colkeys?=? {"Heapsort", "Shellsort", "Bubblesort", "QuickSort", "Radixsort", "Simplesort"};???????????????? Double[][]?data?=? {{Timechat.wtime_heap,timechat.wtime_shell,timechat.wtime_bubble,timechat.wtime_quick,timechat.wtime_radix, Timechat.wtime_simple},};?????????? System.out.println (timechat.btime_heap);???????? Return? Datasetutilities.createcategorydataset (rowkeys,?colkeys,?data);?????}?????}
After you've built the chart, you need to use the Chartpanel class to display it, or you can save the resulting chart to a local image format, and I choose to show it directly:
Public?class? timechart{????? Public?static?void?main (string[]?strings) {?????????? Timechat?tc?=?new? Timechat ();? Another class that initializes a variable in data[][]?????????? Chart?ct?=?new?chart ();?????????? Chartpanel?chartf?=?new? Chartpanel (ct.freechart,true);?????????? Chartpanel?chartf1?=?new? Chartpanel (ct.freechart1,true);?????????? Jframe?jf?=?new? JFrame ();?????????? Jf.add (chartf,borderlayout.west);?????????? Jf.add (chartf1,borderlayout.east);?????????? Jf.setvisible (true);?????????? Jf.setsize (1400,?600);?????????? Jf.setlocationrelativeto (null);?????}}
Run the following effect
Java Draw Line Chart