Use Jfreechart to draw a chart in the Web

Source: Internet
Author: User
Tags java web

Use Jfreechart to draw a chart, you can draw two-dimensional histogram, three-dimensional histogram, line chart, as well as pie chart, there are many on the Internet Jfreechart image source, but still need to summarize the process themselves.

It's no stranger to charts, we sort the data in Office Word and finally let ourselves make a chart of the contents of the subtotals. And in Java Web Development, we met again, there are a variety of summary options for charts, where Jfreechart is used.

1, the development environment constructs.

Like the image upload feature, Jfreechart is an open source project that we can download from the Web. Http://www.jfree.org/jfreechart/index.html, we can extract the downloaded compressed package to see the following directory structure:

Extract the jar packages from Lib to our project's Web-inf/lib directory.

2, write code, display the chart of the servlet.

Package com.bjpowernode.drp.statreport.web;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.text.DecimalFormat;

Import Java.text.NumberFormat;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

Import Javax.servlet.http.HttpServlet;
Import Org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.labels.StandardPieSectionLabelGenerator;
Import Org.jfree.chart.plot.PiePlot3D;

Import Org.jfree.data.general.DefaultPieDataset; /** * Demo Direct output chart via servlet * @author Winter Lau/public class Chartdemoservlet extends HttpServlet {public void Servic
		E (servletrequest req, servletresponse Res) throws Servletexception, IOException {res.setcontenttype ("image/jpeg");
		Defaultpiedataset data = GetDataSet ();
				
		Jfreechart chart = Chartfactory.createpiechart3d ("Fruit yield chart", data, True, false, false);
		Set the percentage of the chart. Pieplot3D plot= (Pieplot3d) Chart.getplot (); Show percentage in Picture: Default//plot.setlabelgenerator (New Standardpiesectionlabelgenerator (
		Standardpietooltipgenerator.default_tooltip_format)); Percentage displayed in Picture: Custom, {0} represents option, {1} represents numeric value, {2} represents proportions, two-bit plot.setlabelgenerator after decimal point (new Standardpiesectionlabelgenerator ("{ 
		0}={1} ({2}) ", Numberformat.getnumberinstance (), New DecimalFormat (" 0% "))); Legend Display percentage: Custom way, {0} represents option, {1} represents numeric value, {2} represents proportion Plot.setlegendlabelgenerator (new Standardpiesectionlabelgenerator ("{0} 
		
		={1} ({2})));
		Prints the chart to the browser.
	Chartutilities.writechartasjpeg (Res.getoutputstream (), 1.0f,chart,400,300,null);  /** * Get a demo simple DataSet object * @return/private static Defaultpiedataset GetDataSet () {Defaultpiedataset DataSet =
		New Defaultpiedataset ();
		Dataset.setvalue ("Apple", 100);
		Dataset.setvalue ("Pear", 200);
		Dataset.setvalue ("Grapes", 300);
		Dataset.setvalue ("Banana", 400);
		Dataset.setvalue ("Litchi", 500);
	return dataset;


 }
}


Configure Chartdemoservlet:

	<servlet>
		<servlet-name>ChartDemoServlet</servlet-name>
		<servlet-class> com.bjpowernode.drp.statreport.web.chartdemoservlet</servlet-class>
	</servlet>
	
	< servlet-mapping>
		<servlet-name>ChartDemoServlet</servlet-name>
		<url-pattern>/ Servlet/statreport/flowcardservlet</url-pattern>
	</servlet-mapping>
		 


This servlet can be displayed in the browser, and we call this servlet:http://localhost:8080/infgman6.0/servlet/statreport/flowcardservlet in the browser, Our chart shows the following:

Then we will, according to their own needs, change the legend, change the chart columns, and then embed in their own pages in the specific location.

The example itself is simple, but once again it feels like the process from unfamiliar to familiar, no contact time, do not dare to start, feel mysterious, when we summon up courage to contact to try, found in fact very simple, someone else wrote the source code, we go to reference this package, and to use, for the wrong place, we go to see the source code, Or look at the Help document, see the specific class or the function of the parameters are what, what the representative, where the error, write this blog is to alert themselves to follow the rules of learning, brave to go on.

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.