基础图表技术(一)

来源:互联网 发布:手机数据线线芯排序 编辑:程序博客网 时间:2024/04/30 17:18
1.java代码package com.lh.util;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Image;import java.awt.Stroke;import java.io.FileInputStream;import javax.imageio.ImageIO;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.title.LegendTitle;import org.jfree.chart.title.TextTitle;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.general.PieDataset;import org.jfree.ui.RectangleEdge;public class ChartUtil {private static PieDataset getPieDataset() {//创建一个饼图表的数据集DefaultPieDataset dataset = new DefaultPieDataset();//向饼图表的数据集添加数据dataset.setValue("A", 200);dataset.setValue("B", 400);dataset.setValue("C", 500);dataset.setValue("Java入门", 500);dataset.setValue("PHP精通", 400);return dataset;}public static JFreeChart getJFreeChart() {//获取数据集PieDataset dataset = getPieDataset();//生成JFreeChart对象JFreeChart chart = ChartFactory.createPieChart("Pie title中国",dataset, true, true, false);chart.setAntiAlias(true); //关闭抗锯齿setPiePlotFont(chart);setPiePoltNum(chart);setOutline(chart);setOutline(chart);setLegendTitle(chart);createPiePlot(chart);return chart;}public static void setPiePlotFont(JFreeChart chart){PiePlot piePlot = (PiePlot) chart.getPlot();piePlot.setLabelFont(new Font("宋体",Font.PLAIN,14));//图表字体TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font("宋体",Font.BOLD,20));//标题字体}public static void setPiePoltNum(JFreeChart chart){PiePlot piePlot = (PiePlot) chart.getPlot();piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}-{2}"));//在图表中显示具体的数值//0 类别名称  1  具体数值  2 百分比   3总值}public static void setBackgroundImage(JFreeChart chart,String imgPath){Image image = null;try {image = ImageIO.read(new FileInputStream(imgPath));} catch (Exception e) {e.printStackTrace();}PiePlot piePlot = (PiePlot) chart.getPlot();piePlot.setBackgroundImage(image);//背景图piePlot.setBackgroundAlpha(1f);//背景透明度piePlot.setBackgroundPaint(Color.white); //背景色}public static void setOutline(JFreeChart chart){PiePlot piePlot = (PiePlot) chart.getPlot();piePlot.setBackgroundPaint(Color.white);Stroke stroke = new BasicStroke(5);piePlot.setOutlineVisible(false); //取消边框piePlot.setOutlineStroke(stroke); //边框笔触piePlot.setOutlinePaint(Color.orange); //边框颜色}public static void setLegendTitle(JFreeChart chart){LegendTitle legendTitle = chart.getLegend();legendTitle.setItemFont(new Font("宋体",Font.PLAIN,12)); //图例字体legendTitle.setBackgroundPaint(Color.orange);legendTitle.setItemPaint(Color.magenta);//图例颜色legendTitle.setBorder(0,0,0,0);//图例边框legendTitle.setPosition(RectangleEdge.BOTTOM);//图例位置}public static void createPiePlot(JFreeChart chart){PiePlot pieplot = (PiePlot) chart.getPlot();pieplot.setExplodePercent("A", 0.1);//分离饼图pieplot.setExplodePercent("B", 0.1);pieplot.setExplodePercent("C", 0.1);pieplot.setCircular(true);//是否椭圆pieplot.setShadowXOffset(20); //阴影效果pieplot.setShadowYOffset(20);pieplot.setSectionOutlineStroke("A",new BasicStroke(3f));//分类边框笔触pieplot.setSectionOutlinesVisible(true);//分类边框显示pieplot.setSectionOutlinePaint("A",Color.BLUE);//分类边框的颜色}}


2.网页

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ page import="org.jfree.chart.servlet.ServletUtilities"  %><%@ page import="com.lh.util.ChartUtil"  %><%@ page import="org.jfree.chart.JFreeChart"  %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>基本饼图</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <%JFreeChart chart = ChartUtil.getJFreeChart();  ChartUtil.setBackgroundImage(chart,application.getRealPath("images/backgroundImage.jpg"));//设置背景图片    String filename = ServletUtilities.saveChartAsJPEG(chart,500,500,session);    String chartUrl = path+"/DisplayChart?filename="+filename;     %>     <center><img alt="" src="<%=chartUrl %>"></center>  </body></html>

3.web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>html5</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <servlet><servlet-name>DisplayChart</servlet-name><servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class></servlet><servlet-mapping><servlet-name>DisplayChart</servlet-name><url-pattern>/DisplayChart</url-pattern></servlet-mapping></web-app>

4.最终运行效果



0 0
原创粉丝点击