示例1为纯Java饼形图实例
示例2为jsp页面饼形图实例
示例3为jsp页面柱形图实例
示例1
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.al.DefaultPieDataset;
public class TestPieChart {
public static void createPieChart() {
//用工厂类创建饼图
JFreeChart pieChart = atePieChart("",
createDataset(), true, true, false);
//用于解决标题中文乱码
TextTitle title=new TextTitle("数据4-8",new Font("宋体", Font.PLAIN, 12));
pieChart.setTitle(title);
//用于解决底部中文乱码
Legend().setItemFont(new Font("宋体",Font.PLAIN,12));
// RenderingHints做文字渲染参数的修改
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
RenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//得到饼图的Plot对象
PiePlot piePlot = (PiePlot) Plot();
setSection(piePlot);
setLabel(piePlot);
setNoDataMessage(piePlot);
setNullAndZeroValue(piePlot);
//将饼图显示在图像界面上
ChartFrame frame = new ChartFrame("TestPieChart", pieChart);
frame.pack();
frame.setVisible(true);
}
public static DefaultPieDataset createDataset() {
//设置数据
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("经济", 0.1);
pieDataset.setValue("政治", 3.63);
pieDataset.setValue("车次", 2.84);
pieDataset.setValue("里程", 2.1);
pieDataset.setValue("秋", 2.80);
return pieDataset;
}
public static void setSection(PiePlot pieplot) {
//设置扇区颜
pieplot.setSectionPaint("经济", new Color(255, 142, 255));
pieplot.setSectionPaint("政治", new Color(210, 128, 223));
pieplot.setSectionPaint("车次", new Color(100, 96, 191));
pieplot.setSectionPaint("里程", new Color(64, 255, 128));
//设置扇区分离显示
pieplot.setExplodePercent("秋", 0.2D);
//设置扇区边框不可见
pieplot.setSectionOutlinesVisible(false);
}
public static void setLabel(PiePlot pieplot) {
//设置扇区标签显示格式:关键字:值(百分比)
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{1}({2} percent)"));
//设置扇区标签颜
pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));
pieplot.setLabelFont((new Font("宋体", Font.PLAIN, 12)));
}
public static void setNoDataMessage(PiePlot pieplot) {
//设置没有数据时显示的信息
pieplot.setNoDataMessage("无数据");
//设置没有数据时显示的信息的字体
pieplot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 14));
//设置没有数据时显示的信息的颜
pieplot.d);
}
public static void setNullAndZeroValue(PiePlot piePlot) {
//设置是否忽略0和null值
piePlot.setIgnoreNullValues(true);
piePlot.setIgnoreZeroValues(true);
}
public static void main(String[] args) {
createPieChart();
}
}
示例2
<%@ page language="java" import="java.io.*" pageEncoding="utf-8"%>
<%@ page import="org.al.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.labels.StandardPieToolTipGenerator"%>
<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%>
<%@ page import="org.ity.StandardEntityCollection"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>图形</title>
</head>
<body>
<%
DefaultPieDataset data = new DefaultPieDataset();
//数据初始化
data.setValue("高中以下",380);
data.setValue(乱码文字生成"高中",1620);
data.setValue("大专",6100);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论