Use Jfree to implement bar charts, Java code

Source: Internet
Author: User

Use Jfree to implement bar chart, Java code. It may be used in production reports, after which the active generation of their own code can be viewed at will. Freely configure the various properties of the chart in order to meet their requirements and objectives
Package Test1;import Org.jfree.chart.*;import Org.jfree.chart.plot.*;import org.jfree.chart.labels.*;import Org.jfree.data.category.*;import Java.awt.*;import Java.io.fileoutputstream;import Java.io.IOException;import Java.sql.connection;import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.Statement;import Java.text.dateformat;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.Date;import Org.jfree.ui.*;import Org.jfree.chart.renderer.category.barrenderer;import Org.jfree.chart.renderer.category.barrenderer3d;import Org.jfree.chart.servlet.*;import Org.jfree.chart.plot.plotorientation;import Org.jfree.data.general.datasetutilities;import com.dao.DBConnection; public class Jfreechart {public static void main (string[] args) throws IOException, SQLException {//TODO auto-generated m  Ethod stubdouble[][] data = new double[][] {{1230, 1110, 1120, 1210, 720, 750, 860, 800, 1230, 1110, 1120,1210, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720,750, 860, 800},{720, 750, 860, 800, 1230, 1110, 1120, 1210, 720, 750, 860,800, 720, 750, 860, 800, 1230, 1110, 1120, 1210, 720 , 750, 860, 800}; String[] Rowkeys = {"A", "B"}; String[] Columnkeys = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", " 18 "," 19 "," 20 "," 21 "," 22 "," 23 "}; Categorydataset DataSet = Datasetutilities.createcategorydataset (Rowkeys, Columnkeys, data); Jfreechart chart = Chartfactory.createbarchart (Getnextday () + "Daily volume", "Time (/h)", "Volume", DataSet, Plotorientation.vertical, True, True,false); Categoryplot plot = Chart.getcategoryplot ();//Set grid background color plot.setbackgroundpaint (color.white);// Sets the grid vertical line Color Plot.setdomaingridlinepaint (color.pink),//sets the grid horizontal line Color plot.setrangegridlinepaint (color.pink);//Displays the value of each bar, and change the Font property of the value barrenderer renderer = new Barrenderer ();//Renderer.setbaseitemlabelgenerator (new// Standardcategoryitemlabelgenerator ());//Renderer.setbaseitemlabelsvisible (TRUE);//The default number is displayed in the column, with the following two sentences for example to adjust the display of numbers Note: This sentence is very important, if there is no such sentence, the display of the number willis overwritten, giving the number of figures not showing up the problem//renderer.setbasepositiveitemlabelposition (new Itemlabelposition (//Itemlabelanchor.outside12, Textanchor.baseline_left));//Renderer.setitemlabelanchoroffset (10D);// Set the distance between the parallel bars included in each region Renderer.setitemmargin (0.05);p lot.setrenderer (renderer);//Set the location, sales display position//place the "callout" below// Plot.setdomainaxislocation (Axislocation.top_or_right)///Place the default on the left side of "sales" to the right//plot.setrangeaxislocation ( Axislocation.bottom_or_right);d rawtooutputstream ("src//a.jpg", chart);} /** * Step3: Output chart to specified disk * * @param destpath * @param chart */public static void Drawtooutputstream (String destpath, Jfree Chart chart) {FileOutputStream fos = null;try {fos = new FileOutputStream (destpath);//Chartutilities.writechartasjpeg ( Chartutilities.writechartaspng (FOS,//Specify target output stream chart,//Chart Object 1000,//Width 500,//high null); Chartrenderinginfo information} catch (IOException e) {e.printstacktrace ();} finally {try {fos.close ();} catch (IOException e) {E . Printstacktrace ();}}} public static String Getnextday () {Calendar calendar = CalendaR.getinstance (); Calendar.add (Calendar.date,-1); Get the day before date = Calendar.gettime ();D ateformat df = new SimpleDateFormat ("Yyyy-mm-dd");//System.out.println ( Df.format (date)); return Df.format (date);}}

Show build Results:


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Use Jfree to implement bar charts, Java code

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.