Java percentage pie chart implementation code

Source: Internet
Author: User

A pie chart showing percentages is as follows:

Implementation code

The code is as follows: Copy code

<% @ Page contentType = "text/html; charset = GBK" %>
 
<% @ Page
Import = "org. jfree. chart. *, org. jfree. chart. plot. PiePlot,
Org. jfree. data. general. DefaultPieDataset,
Org. jfree. chart. servlet. ServletUtilities,
Java. awt. *, org. jfree. chart. title. TextTitle "%>
<% @ Page
Import = "org. jfree. chart. labels. StandardPieSectionLabelGenerator" %>
<% @ Page import = "java. text. NumberFormat" %>
<% @ Page import = "java. text. DecimalFormat" %>
 
<%
// Set the dataset
DefaultPieDataset dataset = new DefaultPieDataset ();
Dataset. setValue ("junior high school senior programmer", 0.52 );
Dataset. setValue ("project manager", 0.1 );
Dataset. setValue ("system Analytics", 0.1 );
Dataset. setValue ("software architect", 0.1 );
Dataset. setValue ("other", 0.18 );
 
// Generate a JFreeChart object through the factory class
JFreeChart chart = ChartFactory. createPieChart3D ("IT industry career distribution chart ",
Dataset, true, false, false );
 
PiePlot pieplot = (PiePlot) chart. getPlot ();
// A highlighted pie chart has not been found on the Internet for a long time. Thank you for your guidance. <? What if I find a solution later?>
// Pieplot. setExplodePercent ("A", 0.3D); // The fried pie chart has some problems.
 
// DecimalFormat:
// NumberFormat:
// StandardPieSectionLabelGenerator:
// SetLabelGenerator ():
DecimalFormat df = new DecimalFormat ("0.00%"); // Obtain a DecimalFormat object, mainly for decimal setting
NumberFormat nf = NumberFormat. getNumberInstance (); // Obtain a NumberFormat object
StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator (
"{0} {2}", nf, df); // Obtain the StandardPieSectionLabelGenerator object
Pieplot. setLabelGenerator (sp); // you can specify the percentage of a pie chart.
 
// Content displayed when no data exists
Pieplot. setNoDataMessage ("no data display ");
Pieplot. setCircular (false );
Pieplot. setLabelGap (0.02D );
 
Pieplot. setIgnoreNullValues (true); // The setting does not display null values.
Pieplot. setIgnoreZeroValues (true); // set not to display negative values
 
// Title text garbled IT industry career distribution chart
TextTitle textTitle = chart. getTitle ();
TextTitle. setFont (new Font ("", Font. PLAIN, 20 ));
 
// Garbled text on the pie
PiePlot plot = (PiePlot) chart. getPlot ();
Plot. setLabelFont (new Font ("", Font. PLAIN, 12 ));
 
// Five descriptions of the legend text garbled pie chart
Chart. getLegend (). setItemFont (new Font ("", Font. PLAIN, 12 ));
 
String filename = ServletUtilities. saveChartAsPNG (Charts, 500,300,
Null, session );
String graphURL = request. getContextPath ()
+ "/DisplayChart? Filename = "+ filename;
%>
 
<Html>
<Head>
<Title> Pie Chart 2 (add a percentage and highlight a block) </title>
</Head>
<Body>
" width = 500 height = 300 border = 0
Usemap = "# <% = filename %>">
</Body>
</Html>

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.