Use jfreechart to generate bar chart, line chart, and pie chart

Source: Internet
Author: User

Use jfreechart to generate bar chart, line chart, and pie chart

JFreeChart is an open class library for drawing charts on the JAVA platform. It is fully written in JAVA and is designed for applications, applets, servlets, and JSP. Next I will detail how to use jfreechart to generate bar charts, line charts, and pie charts.

Steps:

(1) import its corresponding jcommon-1.0.16.jar and jfreechart-1.0.13.jar files (can be clicked to download)

②. You can write the code below to implement various graphics.

A. Generate A column chart:

Package com. whp. test; import java. awt. font; import java. io. file; import java. io. IOException; import org. jfree. chart. chartFactory; import org. jfree. chart. chartFrame; import org. jfree. chart. chartUtilities; import org. jfree. chart. JFreeChart; import org. jfree. chart. axis. categoryAxis3D; import org. jfree. chart. axis. numberAxis3D; import org. jfree. chart. axis. numberTickUnit; import org. jfree. chart. labels. standardCategoryItemLabelGenerator; import org. jfree. chart. plot. categoryPlot; import org. jfree. chart. plot. plotOrientation; import org. jfree. chart. renderer. category. barRenderer3D; import org. jfree. data. category. defaultCategoryDataset; public class jfreeChart {public static void main (String [] args) {// TODO Auto-generated method stubdefadefacategorydataset dataset = new DefaultCategoryDataset (); // Add data dataset. addValue (98, "Mathematics", "Zhang San"); dataset. addValue (87, "", "Zhang San"); dataset. addValue (68, "Mathematics", "Li Si"); dataset. addValue (89, "", ""); dataset. addValue (56, "Mathematics", "Wang Wu"); dataset. addValue (96, "", ""); JFreeChart chart = ChartFactory. createBarChart3D ("score statistical table", "Student name", // tag of the X axis "score", // tag of the Y axis dataset, // set of data displayed by the icon PlotOrientation. VERTICAL, // The image display format (horizontal or VERTICAL) true, // whether to display the sub-title true, // whether to generate the prompt label true ); // generate URL link // process the Garbled text on the image // process the Garbled text chart of the Main title. getTitle (). setFont (new Font ("", Font. BOLD, 18); // process the sub-title garbled chart. getLegend (). setItemFont (new Font ("", Font. BOLD, 15); // obtain the chart region object CategoryPlot categoryPlot = (CategoryPlot) chart. getPlot (); // obtain the X axis object CategoryAxis3D categoryAxis3D = (CategoryAxis3D) categoryPlot. getDomainAxis (); // obtain the object NumberAxis3D numberAxis3D = (NumberAxis3D) categoryPlot of the Y axis. getRangeAxis (); // process garbled characters on the X axis categoryAxis3D. setTickLabelFont (new Font ("", Font. BOLD, 15); // process garbled characters outside the X axis categoryAxis3D. setLabelFont (new Font ("", Font. BOLD, 15); // handle garbled numberAxis3D on the Y axis. setTickLabelFont (new Font ("", Font. BOLD, 15); // handle garbled numberAxis3D outside the Y axis. setLabelFont (new Font ("", Font. BOLD, 15); // process the scale displayed on the Y axis. Use 10 as the numberAxis3D value. setAutoTickUnitSelection (false); NumberTickUnit unit = new NumberTickUnit (10); numberAxis3D. setTickUnit (unit); // obtain the plotting area object BarRenderer3D barRenderer3D = (BarRenderer3D) categoryPlot. getRenderer (); // set the barRenderer3D bar width. setMaximumBarWidth (0.07); // display the number barRenderer3D on the graph. setBaseItemLabelGenerator (new StandardCategoryItemLabelGenerator (); barRenderer3D. setBaseItemLabelsVisible (true); barRenderer3D. setBaseItemLabelFont (new Font ("", Font. BOLD, 15); // generate the image File = new file ("chart.jpeg") under the d directory; try {ChartUtilities. saveChartAsJPEG (file, chart, 800,600);} catch (IOException e) {e. printStackTrace ();} // use the ChartFrame object to display the image ChartFrame = new ChartFrame ("xyz", chart); frame. setVisible (true); frame. pack ();}}
Result:



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + QqGiyfqzydXbz9/NvDwvcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> package com. whp. test; import java. awt. font; import java. awt. rectangle; import java. io. file; import java. io. IOException; import org. jfree. chart. chartFactory; import org. jfree. chart. chartFrame; import org. jfree. chart. chartUtilities; import org. jfree. chart. JFreeChart; import org. jfree. chart. axis. categoryAxis; import org. jfree. chart. axis. categoryAxis3D; import org. jfree. chart. axis. numberAxis; import org. jfree. chart. axis. numberAxis3D; import org. jfree. chart. axis. numberTickUnit; import org. jfree. chart. labels. standardCategoryItemLabelGenerator; import org. jfree. chart. plot. categoryPlot; import org. jfree. chart. plot. plotOrientation; import org. jfree. chart. renderer. category. barRenderer3D; import org. jfree. chart. renderer. category. lineAndShapeRenderer; import org. jfree. data. category. defaultCategoryDataset; public class jfreeChart1 {public static void main (String [] args) {// TODO Auto-generated method stubdefadefacategorydataset dataset = new DefaultCategoryDataset (); // Add data dataset. addValue (98, "Mathematics", "Zhang San"); dataset. addValue (68, "Mathematics", "Li Si"); dataset. addValue (56, "Mathematics", "Wang Wu"); JFreeChart chart = ChartFactory. createLineChart ("user statistics report (unit)", // name of the Main title "unit name", // Number of labels on the X axis ", // label dataset on the Y axis, // set of data displayed by the icon: PlotOrientation. VERTICAL, // The image display format (horizontal or VERTICAL) true, // whether to display the sub-title true, // whether to generate the prompt label true ); // generate URL link // process the Garbled text on the image // process the Garbled text chart of the Main title. getTitle (). setFont (new Font ("", Font. BOLD, 18); // process the sub-title garbled chart. getLegend (). setItemFont (new Font ("", Font. BOLD, 15); // obtain the chart region object CategoryPlot categoryPlot = (CategoryPlot) chart. getPlot (); // obtain the object CategoryAxis categoryAxis = (CategoryAxis) categoryPlot of the X axis. getDomainAxis (); // obtain the object NumberAxis numberAxis = (NumberAxis) categoryPlot of the Y axis. getRangeAxis (); // process garbled characters on the X axis categoryAxis. setTickLabelFont (new Font ("", Font. BOLD, 15); // process garbled characters outside the X axis categoryAxis. setLabelFont (new Font ("", Font. BOLD, 15); // handle garbled numberAxis on the Y axis. setTickLabelFont (new Font ("", Font. BOLD, 15); // handle garbled numberAxis outside the Y axis. setLabelFont (new Font ("", Font. BOLD, 15); // process the scale displayed on the Y axis, using 10 as the 1-digit numberAxis. setAutoTickUnitSelection (false); NumberTickUnit unit = new NumberTickUnit (10); numberAxis. setTickUnit (unit); // obtain the LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot of the drawing area object. getRenderer (); // display the number lineAndShapeRenderer in the graph. setBaseItemLabelGenerator (new StandardCategoryItemLabelGenerator (); lineAndShapeRenderer. setBaseItemLabelsVisible (true); lineAndShapeRenderer. setBaseItemLabelFont (new Font ("", Font. BOLD, 15); // Add a turning point on the graph (displayed with a small Rectangle) Rectangle shape = new Rectangle (10, 10); lineAndShapeRenderer. setSeriesShape (0, shape); lineAndShapeRenderer. setSeriesShapesVisible (0, true); // generate an image File file = new File ("chart1.jpg") in the d directory; try {ChartUtilities. saveChartAsJPEG (file, chart, 800,600);} catch (IOException e) {e. printStackTrace ();} // use the ChartFrame object to display the image ChartFrame = new ChartFrame ("xyz", chart); frame. setVisible (true); frame. pack ();}}
Result:


C. Generate a pie chart:

Package com. whp. test; import java. awt. font; import java. awt. rectangle; import java. io. file; import java. io. IOException; import org. jfree. chart. chartFactory; import org. jfree. chart. chartFrame; import org. jfree. chart. chartUtilities; import org. jfree. chart. JFreeChart; import org. jfree. chart. axis. categoryAxis; import org. jfree. chart. axis. categoryAxis3D; import org. jfree. chart. axis. numberAxis; import org. jfree. chart. axis. numberAxis3D; import org. jfree. chart. axis. numberTickUnit; import org. jfree. chart. labels. standardCategoryItemLabelGenerator; import org. jfree. chart. labels. standardPieSectionLabelGenerator; import org. jfree. chart. plot. categoryPlot; import org. jfree. chart. plot. piePlot3D; import org. jfree. chart. plot. plotOrientation; import org. jfree. chart. renderer. category. barRenderer3D; import org. jfree. chart. renderer. category. lineAndShapeRenderer; import org. jfree. data. category. defaultCategoryDataset; import org. jfree. data. general. defaultPieDataset; public class jfreeChart3 {public static void main (String [] args) {// TODO Auto-generated method stubdefadefapiedataset dataset = new DefaultPieDataset (); // Add data dataset. setValue ("Zhang San", 40); dataset. setValue ("Li Si", 32); dataset. setValue ("Wang Wu", 28); JFreeChart chart = ChartFactory. createPieChart3D ("proportion statistics report (unit)", // The Name Of The main title dataset, // set of data displayed by the icon true, // whether to display the subtitle true, // whether to generate the prompt label true); // whether to generate the URL link // process the garbled chart on the image // process the garbled chart of the Main title. getTitle (). setFont (new Font ("", Font. BOLD, 18); // process the sub-title garbled chart. getLegend (). setItemFont (new Font ("", Font. BOLD, 15); // obtain the chart area object PiePlot3D categoryPlot = (PiePlot3D) chart. getPlot (); // process garbled categoryPlot on the image. setLabelFont (new Font ("", Font. BOLD, 15); // set the image generation format to (Shanghai 2 (10%) String format = "{0} {1} ({2})"; categoryPlot. setLabelGenerator (new StandardPieSectionLabelGenerator (format); // generate an image File file = new File ("chart2.jpg") in the d directory; try {ChartUtilities. saveChartAsJPEG (file, chart, 800,600);} catch (IOException e) {e. printStackTrace ();} // use the ChartFrame object to display the image ChartFrame = new ChartFrame ("xyz", chart); frame. setVisible (true); frame. pack ();}}

Result:





Related Article

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.