- 浏览: 246093 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
wilsonchen:
...
pdf.js html5展现pdf -
angole:
不错,又学习到了。
mybatis打印sql -
hft24dq:
rxcss66 写道 个人没有搞明白什么原理,不过跟一楼说的一 ...
mybatis打印sql -
fireinjava:
fireinjava 写道org.apache.ibatis. ...
mybatis打印sql -
fireinjava:
org.apache.ibatis.logging.LogFa ...
mybatis打印sql
关键代码:
参考网站http://balajinatarajan.blogspot.com/2008/04/customizing-jfreechart-pie-chart.html
Introduction
Dynamic representation of data in picture format is inevitable in this programming era. JFreeChart is one of the best open sources in Java World in providing APIs to display dynamic charts viz., Line Chart, Bar Chart, Pie Chart etc., A wide collection of APIs are available under LGPL license not only to generate different charts but also to customize the generated charts with our own look and feel. My projects are basically Portal Applications that involve displaying a Pie Chart for the backend data. As the creation of pie chart just takes two or three lines, I felt it is important to explore more ways to customize the chart generated and the result is this blog. Following section briefly describes various ways to customize pie chart generated using JFreeChart.
Getting Started
For generating and displaying JFreeCharts we need to include certain libraries which are available in JFreeChart web page.
Place these jar files in your WEB-INF/lib folder:
1.jfreechart-*.jar
2.jcommon-*.jar
Creating a Pie Chart using JFreeChart libraries
Creation of pie chart won’t take you too many lines as it mainly needs the dataset for which you need to generate the chart apart from other details like Chart Title, Flag to say whether we need Legend or not etc., Following code shows a way to create a pie chart with available dataset and customize it in many available ways.
1: import java.awt.Color;
2: import java.text.AttributedString;
3: import java.util.List;
4: import org.jfree.chart.ChartFactory;
5: import org.jfree.chart.JFreeChart;
6: import org.jfree.chart.labels.PieSectionLabelGenerator;
7: import org.jfree.chart.plot.PiePlot;
8: import org.jfree.chart.plot.Plot;
9: import org.jfree.chart.title.TextTitle;
10: import org.jfree.data.general.DefaultPieDataset;
11: import org.jfree.data.general.PieDataset;
12: import org.jfree.ui.RectangleInsets;
13: import org.jfree.util.UnitType;
/*
* This class is used to create a sample pie chart using JFreeChart and customize it
*/
14: public class CustomizePieChart {
15: public static JFreeChart customizeSamplePieChart( ) {
/* Creating a sample Pie Dataset */
16: final DefaultPieDataset data =
new DefaultPieDataset ( );
/* Storing the sample Pie Chart Data in Pie dataset */
17: data.setValue("Software Engineer",50);
18: data.setValue("Programmer Analyst",20);
19: data.setValue("Technical Specialist",25);
20: data.setValue("Project Manager",10);
/* Create a Pie Chart */
/* Let the Legend be present */
21: final boolean withLegend = true;
22: final JFreeChart chart =
ChartFactory.createPieChart("Employee Survey",
data, withLegend, true, true );
/* Creating a pieplot so as to customize the chart generated */
23: final PiePlot plot = (PiePlot)chart.getPlot( );
/* Customizing Label using an inner class */
24: plot.setLabelGenerator(new CustomLabelGenerator( ));
/* Setting the BackGround Paint for Label */
25: plot.setLabelBackgroundPaint(Color.WHITE);
Customizing a Pie Chart by creating a PiePlot
In order to customize a pie chart we need to create a plot of type PiePlot. PiePlot extends basic Plot and this plot instance is provided by a static method of JFreeChart. Once a plot is obtained we can do dozens of customizations.
Customizing Pie Chart’s label
If you have a view at dataset for which the chart will be generated, it is just a collection of (name, value) pairs. Pie chart is generated for value parameter (which must be parsable into double) whereas labels and legends are displayed based on the name given. In order to customize label, we need to create an inner class within the class in which the pie chart is generated and pass an instance of that inner class (where we set some rules of how to display the label) to a method in PiePlot which displays the label the way we defined it in the inner class. See Line no. 24 for the PiePlot’s set method and Line no. 39 for static inner class that defines how label should be displayed.
We can also customize other properties of label using PiePlot. This includes,
1.Setting Label Background
2.Setting Label outline
3.Setting Label shadow
4.Disabling label links etc.,
Customizing Pie Chart’s legend
A pie chart’s legend is optional and if we wish to have, we can also customize it the way we did for label. See Line no. 28 for PiePlot’s set method and line no. 46 for static inner class that defines how legend should be displayed. We can also set the Legend items’ shape to pre-defined shapes and the default one is circle.
/* Setting the Outline paint for the labels */
23: plot.setLabelOutlinePaint(Color.WHITE);
/* Setting the Label Shadow Paint */
24: plot.setLabelShadowPaint(Color.WHITE);
/* Setting Legend Item Shape (Default is Circle Shape) */
25: plot.setLegendItemShape(Plot.DEFAULT_LEGEND_ITEM_BOX);
26: plot.setBaseSectionOutlinePaint(Color.LIGHT_GRAY);
27: plot.setShadowPaint(Color.WHITE);
/* Customizing Label using an inner class */
28: plot.setLegendLabelGenerator(new CustomLegendGenerator());
/* Creating a Title Object and setting its value */
29: TextTitle title =
new TextTitle("Employee Survey");
/* Setting the Background Paint for the title */
30: title.setBackgroundPaint(new Color(0.294f, 0.0f, 0.513f));
/* Setting the font paint for the title */
31: title.setPaint(new
Color(1.0f, 0.549f, 0.0f));
/* Expanding the title to the entire width of the pie chart image */
32: title.setExpandToFitSpace(true);
/* Setting the title instance to the chart, atlast */
33: chart.setTitle(title);
/* Setting insets to the pie chart image */
34: plot.setInsets(new
RectangleInsets(UnitType.ABSOLUTE, 0.0, 0.0,1.0, 1.0));
/* Specifying list of colors from which a Pie Section should be painted based on our choice */
35: Color[] colors = {Color.blue, Color.yellow,
Color.green, Color.GRAY};
/* Delegating the choice of color to an inner class */
36: PieRenderer renderer = new PieRenderer(colors);
37: renderer.setColor(plot, data);
/* Returns the chart with customization */
38: return chart;
}
Customizing Pie Chart’s Title
A pie chart’s title is defined when a new JFreeChart instance is created. We can also customize the same by creating an instance of type TextTitle and setting properties on created instance. See line no. 29 to 33 for details.
Customizing Pie section colors
When you create a default pie chart, all pie sections will be painted in system selected colors which are random. If you wish to define which pie section should be painted on which color, well, you can do that by creating another inner class that defines the exact selection of colors. See line no. 36 for 54 for more details.
Miscellaneous customizations
Apart from aforementioned customizations, following are the miscellaneous ones that I hope will be helpful for you.
Setting insets
Insets for a pie chart can be customized and we can even set ZERO inset.
See line no. 34 for ZERO insets.
Setting paints for BaseSection and Shadow.
39: static class CustomLabelGenerator implements PieSectionLabelGenerator {
/**
* Set customized label in the pie chart
*
* @param dataset PieChart DataSet
* @param key Comparable Key
* @return String Result String to be displayed
*/
40: public String generateSectionLabel(final PieDataset dataset,
final Comparable key) {
/* Creating a temporary string to hold value defined by us */
41: String temp = null;
42: if (dataset != null) {
/* Assigning the Value from dataset as a label for display */
43: temp = dataset.getValue(key).toString();
/* Modifying a particular label based on the Key name */
44: if(key.toString().equalsIgnoreCase("Software
Engineer")) {
temp = temp + "(Trainees Not Included)";
}
}
/* Returning the formatted string back to set the label */
45: return temp;
}
}
46: static class CustomLegendGenerator
implements PieSectionLabelGenerator {
/**
* Set customized legend in the pie chart
*
* @param dataset PieChart DataSet
* @param key Comparable Key
* @return Result String to be displayed
*/
47: public String
generateSectionLabel(final PieDataset dataset,
final Comparable key) {
/* Creating a temporary string to hold value defined by us */
48: String temp = null;
49: if (dataset != null) {
/* Assigning the Value from dataset as a legend item for display */
50: temp = key.toString();
/* Modifying a particular legend item based on the Key name */
51: if(key.toString().equalsIgnoreCase("Software
Engineer")) {
52: temp = temp + "(Trainees
Not Included)";
}
}
/* Returning the formatted string back to set the label */
53: return temp;
}
}
54: static class PieRenderer
{
/* Declaring an array of Color variables for storing a list of Colors */
55: private Color[ ]
color;
/* Constructor to initialize PieRenderer class */
56: public PieRenderer(Color[ ] color)
{
57: this.color = color;
}
/**
* Set Method to set colors for pie sections based on our choice
* @param plot PiePlot of PieChart
* @param dataset PieChart DataSet
*/
58: public void setColor(PiePlot plot,
DefaultPieDataset dataset)
{
59: List keys = dataset.getKeys();
60: int aInt;
61: for (int i =
0; i <
keys.size(); i++)
{
62: aInt = i % this.color.length;
63: plot.setSectionPaint(
keys.get(i).toString(),this.color[aInt]);
}
}
}
}
Generated Output
Following are the sample outputs that will show differences between a generic and customized chart. Since the project I involved is basically a web application I used a servlet to display all these charts.
Fig 1: Generic Chart – Not Customized Fig 2: Chart with customized label
Fig 3: Chart with customized legend Fig 4: Chart with customized title
Fig 5: Chart with ZERO insets Fig 6: Chart with custom pie section colors
Note: Copy the war file into the webapps folder of the Tomcat. Start the Tomcat server & view the charts (JSP) through a browser. [In the address bar give: http://localhost:/CustomizePieChart]
private static class PieRenderer { private static final Paint[] COLORS = { Color.RED, Color.YELLOW, Color.GRAY, Color.MAGENTA, Color.ORANGE, Color.GREEN, Color.DARK_GRAY, Color.CYAN, Color.PINK, Color.BLUE, Color.LIGHT_GRAY }; public void setColor(PiePlot plot, CategoryDataset dataset) { List keys = dataset.getRowKeys(); for (int i = 0; i < keys.size(); i++) { plot.setSectionPaint(keys.get(i).toString(), COLORS[i % COLORS.length]); } } } PieRenderer renderer = new PieRenderer(); renderer.setColor(localPiePlot, paramCategoryDataset);
参考网站http://balajinatarajan.blogspot.com/2008/04/customizing-jfreechart-pie-chart.html
Introduction
Dynamic representation of data in picture format is inevitable in this programming era. JFreeChart is one of the best open sources in Java World in providing APIs to display dynamic charts viz., Line Chart, Bar Chart, Pie Chart etc., A wide collection of APIs are available under LGPL license not only to generate different charts but also to customize the generated charts with our own look and feel. My projects are basically Portal Applications that involve displaying a Pie Chart for the backend data. As the creation of pie chart just takes two or three lines, I felt it is important to explore more ways to customize the chart generated and the result is this blog. Following section briefly describes various ways to customize pie chart generated using JFreeChart.
Getting Started
For generating and displaying JFreeCharts we need to include certain libraries which are available in JFreeChart web page.
Place these jar files in your WEB-INF/lib folder:
1.jfreechart-*.jar
2.jcommon-*.jar
Creating a Pie Chart using JFreeChart libraries
Creation of pie chart won’t take you too many lines as it mainly needs the dataset for which you need to generate the chart apart from other details like Chart Title, Flag to say whether we need Legend or not etc., Following code shows a way to create a pie chart with available dataset and customize it in many available ways.
1: import java.awt.Color;
2: import java.text.AttributedString;
3: import java.util.List;
4: import org.jfree.chart.ChartFactory;
5: import org.jfree.chart.JFreeChart;
6: import org.jfree.chart.labels.PieSectionLabelGenerator;
7: import org.jfree.chart.plot.PiePlot;
8: import org.jfree.chart.plot.Plot;
9: import org.jfree.chart.title.TextTitle;
10: import org.jfree.data.general.DefaultPieDataset;
11: import org.jfree.data.general.PieDataset;
12: import org.jfree.ui.RectangleInsets;
13: import org.jfree.util.UnitType;
/*
* This class is used to create a sample pie chart using JFreeChart and customize it
*/
14: public class CustomizePieChart {
15: public static JFreeChart customizeSamplePieChart( ) {
/* Creating a sample Pie Dataset */
16: final DefaultPieDataset data =
new DefaultPieDataset ( );
/* Storing the sample Pie Chart Data in Pie dataset */
17: data.setValue("Software Engineer",50);
18: data.setValue("Programmer Analyst",20);
19: data.setValue("Technical Specialist",25);
20: data.setValue("Project Manager",10);
/* Create a Pie Chart */
/* Let the Legend be present */
21: final boolean withLegend = true;
22: final JFreeChart chart =
ChartFactory.createPieChart("Employee Survey",
data, withLegend, true, true );
/* Creating a pieplot so as to customize the chart generated */
23: final PiePlot plot = (PiePlot)chart.getPlot( );
/* Customizing Label using an inner class */
24: plot.setLabelGenerator(new CustomLabelGenerator( ));
/* Setting the BackGround Paint for Label */
25: plot.setLabelBackgroundPaint(Color.WHITE);
Customizing a Pie Chart by creating a PiePlot
In order to customize a pie chart we need to create a plot of type PiePlot. PiePlot extends basic Plot and this plot instance is provided by a static method of JFreeChart. Once a plot is obtained we can do dozens of customizations.
Customizing Pie Chart’s label
If you have a view at dataset for which the chart will be generated, it is just a collection of (name, value) pairs. Pie chart is generated for value parameter (which must be parsable into double) whereas labels and legends are displayed based on the name given. In order to customize label, we need to create an inner class within the class in which the pie chart is generated and pass an instance of that inner class (where we set some rules of how to display the label) to a method in PiePlot which displays the label the way we defined it in the inner class. See Line no. 24 for the PiePlot’s set method and Line no. 39 for static inner class that defines how label should be displayed.
We can also customize other properties of label using PiePlot. This includes,
1.Setting Label Background
2.Setting Label outline
3.Setting Label shadow
4.Disabling label links etc.,
Customizing Pie Chart’s legend
A pie chart’s legend is optional and if we wish to have, we can also customize it the way we did for label. See Line no. 28 for PiePlot’s set method and line no. 46 for static inner class that defines how legend should be displayed. We can also set the Legend items’ shape to pre-defined shapes and the default one is circle.
/* Setting the Outline paint for the labels */
23: plot.setLabelOutlinePaint(Color.WHITE);
/* Setting the Label Shadow Paint */
24: plot.setLabelShadowPaint(Color.WHITE);
/* Setting Legend Item Shape (Default is Circle Shape) */
25: plot.setLegendItemShape(Plot.DEFAULT_LEGEND_ITEM_BOX);
26: plot.setBaseSectionOutlinePaint(Color.LIGHT_GRAY);
27: plot.setShadowPaint(Color.WHITE);
/* Customizing Label using an inner class */
28: plot.setLegendLabelGenerator(new CustomLegendGenerator());
/* Creating a Title Object and setting its value */
29: TextTitle title =
new TextTitle("Employee Survey");
/* Setting the Background Paint for the title */
30: title.setBackgroundPaint(new Color(0.294f, 0.0f, 0.513f));
/* Setting the font paint for the title */
31: title.setPaint(new
Color(1.0f, 0.549f, 0.0f));
/* Expanding the title to the entire width of the pie chart image */
32: title.setExpandToFitSpace(true);
/* Setting the title instance to the chart, atlast */
33: chart.setTitle(title);
/* Setting insets to the pie chart image */
34: plot.setInsets(new
RectangleInsets(UnitType.ABSOLUTE, 0.0, 0.0,1.0, 1.0));
/* Specifying list of colors from which a Pie Section should be painted based on our choice */
35: Color[] colors = {Color.blue, Color.yellow,
Color.green, Color.GRAY};
/* Delegating the choice of color to an inner class */
36: PieRenderer renderer = new PieRenderer(colors);
37: renderer.setColor(plot, data);
/* Returns the chart with customization */
38: return chart;
}
Customizing Pie Chart’s Title
A pie chart’s title is defined when a new JFreeChart instance is created. We can also customize the same by creating an instance of type TextTitle and setting properties on created instance. See line no. 29 to 33 for details.
Customizing Pie section colors
When you create a default pie chart, all pie sections will be painted in system selected colors which are random. If you wish to define which pie section should be painted on which color, well, you can do that by creating another inner class that defines the exact selection of colors. See line no. 36 for 54 for more details.
Miscellaneous customizations
Apart from aforementioned customizations, following are the miscellaneous ones that I hope will be helpful for you.
Setting insets
Insets for a pie chart can be customized and we can even set ZERO inset.
See line no. 34 for ZERO insets.
Setting paints for BaseSection and Shadow.
39: static class CustomLabelGenerator implements PieSectionLabelGenerator {
/**
* Set customized label in the pie chart
*
* @param dataset PieChart DataSet
* @param key Comparable Key
* @return String Result String to be displayed
*/
40: public String generateSectionLabel(final PieDataset dataset,
final Comparable key) {
/* Creating a temporary string to hold value defined by us */
41: String temp = null;
42: if (dataset != null) {
/* Assigning the Value from dataset as a label for display */
43: temp = dataset.getValue(key).toString();
/* Modifying a particular label based on the Key name */
44: if(key.toString().equalsIgnoreCase("Software
Engineer")) {
temp = temp + "(Trainees Not Included)";
}
}
/* Returning the formatted string back to set the label */
45: return temp;
}
}
46: static class CustomLegendGenerator
implements PieSectionLabelGenerator {
/**
* Set customized legend in the pie chart
*
* @param dataset PieChart DataSet
* @param key Comparable Key
* @return Result String to be displayed
*/
47: public String
generateSectionLabel(final PieDataset dataset,
final Comparable key) {
/* Creating a temporary string to hold value defined by us */
48: String temp = null;
49: if (dataset != null) {
/* Assigning the Value from dataset as a legend item for display */
50: temp = key.toString();
/* Modifying a particular legend item based on the Key name */
51: if(key.toString().equalsIgnoreCase("Software
Engineer")) {
52: temp = temp + "(Trainees
Not Included)";
}
}
/* Returning the formatted string back to set the label */
53: return temp;
}
}
54: static class PieRenderer
{
/* Declaring an array of Color variables for storing a list of Colors */
55: private Color[ ]
color;
/* Constructor to initialize PieRenderer class */
56: public PieRenderer(Color[ ] color)
{
57: this.color = color;
}
/**
* Set Method to set colors for pie sections based on our choice
* @param plot PiePlot of PieChart
* @param dataset PieChart DataSet
*/
58: public void setColor(PiePlot plot,
DefaultPieDataset dataset)
{
59: List keys = dataset.getKeys();
60: int aInt;
61: for (int i =
0; i <
keys.size(); i++)
{
62: aInt = i % this.color.length;
63: plot.setSectionPaint(
keys.get(i).toString(),this.color[aInt]);
}
}
}
}
Generated Output
Following are the sample outputs that will show differences between a generic and customized chart. Since the project I involved is basically a web application I used a servlet to display all these charts.
Fig 1: Generic Chart – Not Customized Fig 2: Chart with customized label
Fig 3: Chart with customized legend Fig 4: Chart with customized title
Fig 5: Chart with ZERO insets Fig 6: Chart with custom pie section colors
Note: Copy the war file into the webapps folder of the Tomcat. Start the Tomcat server & view the charts (JSP) through a browser. [In the address bar give: http://localhost:/CustomizePieChart]
发表评论
-
spring send gmail
2012-04-24 11:06 1084只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1044原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1177原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5741情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 896http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1058几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1572原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1530原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3091总结:HPack 优于 CJSON json1 json2 ... -
Why we don’t use Doubles for Financial Calculations
2011-06-27 11:14 1244原文地址:http://bloodredsun.com/?p= ... -
http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
2011-06-27 10:48 1181原文地址:http://jbaruch.wordpress.c ... -
Sturts2 全局异常日志
2011-06-25 10:50 1780原文地址:http://www.brucephillips.n ... -
eclipse的build与clean
2011-06-22 09:01 1542现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 794图标各部分名字 -
jfreechart demo 代码
2011-06-07 18:33 2919jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 902基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 987collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1039最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1554修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 19892mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
JFreeChart生成饼图
20201012JFreeChart生成饼图和环图技术.zip
使用JFreeChart绘制饼图,折线图,柱状图的代码试例
该程序为一个绘制饼图的简单例子,并解决了再饼图中汉字显示出现乱码问题。
JFreeChart 饼图、柱状图 例 (含数据库) MyEclipse版
最简单的饼图制作,需要两个jar包,代码直接copy运行.
JavaSE学习时候都可以看这个例子了,注意几个标题区别,别的没什么可说的。 我在做得时候发现不支持汉字,这个我想可以解决的。 我会抽空仔细看看代码~\(≧▽≦)/~啦啦啦 ...jfreechart饼图例子
给出饼图标题,饼图每块的名字和值存入map中,在讲每个map存入List中,调用类中的方法即可生成饼图
做饼图需要的两个jar包: jcommon-1.0.0-rc1.jar和jfreechart-1.0.0-rc1.jar。
jfreechart ,fusionchart 饼图 折线图 等实例
JFreeChart 饼图,柱图,折线图可运行
JFreeChart饼图、折线图等资料学习,jar包,示例,api。全有非常好用
java绘制图表(柱状图、饼图、折线图)包, jfreechart
JAR是重新编译过得,参考博客:http://blog.sina.com.cn/s/blog_a7f7f4e10102v3c0.html
jfreechart-1.0.8a-swt.jar jfreechart-1.0.8a.jar jfreechart-1.0.8a-experimental.jar jcommon-1.0.12.jar gnujaxp.jar 及网上找到的简单的饼图和柱状图的用例(jfreechart.exe帮助文件)
在springboot的web项目上,利用jfreeChart+java+thymeleaf(thymeleaf也可以换成jstl)制作折线图,柱形图,3d柱形图,多组柱形图,圆饼图,如果要演示到web项目和页面上去的可以下载看看
投票系统struts2.0+hibernate3.2+spring2.5+sqlserver2005数据库,+JFReeChart饼图,柱状图显示。
NULL 博文链接:https://dpn525.iteye.com/blog/847847
JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够 用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。本文将通过引领...
jfreechart 图形折线,饼图,柱状图等各种图形