package com.chart.test;
代碼如下:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import oracle.jrockit.jfr.openmbean.JFRStatsType;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
public class ChartTest
{
public static void main(String[] args){
ChartTest test=new ChartTest();
test.generateFile(test.getChart());
}
public JFreeChart getChart(){
String series1 ="First";
String series2 ="Second";
String type1 ="Type 1";
String type2 ="Type 2";
String type3 ="Type 3";
String type4 ="Type 4";
String type5 ="Type 5";
String type6 ="Type 6";
String type7 ="Type 7";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, series1, type1);
defaultcategorydataset.addValue(4D, series1, type2);
defaultcategorydataset.addValue(3D, series1, type3);
defaultcategorydataset.addValue(5D, series1, type4);
defaultcategorydataset.addValue(5D, series1, type5);
defaultcategorydataset.addValue(7D, series1, type6);
defaultcategorydataset.addValue(7D, series1, type7);
defaultcategorydataset.addValue(5D, series2, type1);
defaultcategorydataset.addValue(7D, series2, type2);
defaultcategorydataset.addValue(6D, series2, type3);
defaultcategorydataset.addValue(8D, series2, type4);
defaultcategorydataset.addValue(4D, series2, type5);
defaultcategorydataset.addValue(4D, series2, type6);
defaultcategorydataset.addValue(2D, series2, type7);
JFreeChart jfreechart = ChartFactory.createLineChart("Line Chart Demo 1","Type","Value",defaultcategorydataset,PlotOrientation.VERTICAL,true,true,false);
// ChartFactory.createLineChart(title, categoryAxisLabel, valueAxisLabel, dataset, orientation, legend, tooltips, urls)
// 上面語句legend設置為false時,表示新建的jfreechart無圖例,為true時,自帶圖例,默認位置為底部。
jfreechart.getLegend().setPosition(RectangleEdge.RIGHT);//獲取圖例,設置位置
// LegendTitle legendTitle = new LegendTitle(jfreechart.getPlot());//創建圖例,若有默認的,仍然創建,此時存在兩個。
// legendTitle.setPosition(RectangleEdge.RIGHT);//設置圖例的位置
// jfreechart.addLegend(legendTitle);//圖表中添加圖例
jfreechart.setBackgroundPaint(Color.white);//設置圖例的背景色
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setDomainGridlinePaint(Color.white);//設置橫坐標網格線
categoryplot.setRangeGridlinePaint(Color.white);//設置縱坐標網格線
// 1、對標題
Font font1 = new Font("黑體",Font.BOLD, 14); // 設定字體、類型、字號
jfreechart.getTitle().setFont(font1); // 標題
// 3、下面的方塊區域是 LegendTitle 對象
Font font3 = new Font("隸書",Font.BOLD, 12); // 設定字體、類型、字號
jfreechart.getLegend().setItemFont(font3);// 最下方
// 這是處理Plot裡面的橫軸,同理可以正理豎軸
CategoryAxis categoryaxis = categoryplot.getDomainAxis(); // 橫軸上的
//縱坐標換行顯示
// categoryaxis.setMaximumCategoryLabelLines(10);//行數,根據需要自己設
// categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//每行寬度,這裡設一個漢字寬
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 45度傾斜,可以改成其他,默認是水平
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());// 數據軸的數據標簽(可以只顯示整數標簽,需要將AutoTickUnitSelection設false)
numberaxis.setAutoRangeIncludesZero(true);//是否強制在自動選擇的數據范圍中包含0
categoryaxis.setTickLabelFont(new Font("SansSerif",Font.BOLD, 12));//坐標軸標尺值字體
numberaxis.setTickLabelFont(new Font("SimSun", Font.BOLD, 12));//
// 2、對圖裡面的漢字設定,也就是Plot的設定
Font font2 = new Font("SimSun", Font.BOLD, 16); // 設定字體、類型、字號
categoryaxis.setLabelFont(font2);//坐標軸標題字體
numberaxis.setLabelFont(font2);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //series 點(即數據點)可見
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //定義series為"First"的(即series1)點之間的連線 ,這裡是虛線,默認是直線
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}, 0.0F)); //定義series為"Second"的(即series2)點之間的連線
return jfreechart; //返回Jfreechart
}
public void generateFile(JFreeChart chart){
String fileName = null;
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\PieChart.jpg"),chart,600,600); //生成圖表文件到指定位置
} catch (IOException e)
{
e.printStackTrace();
}
}
public String getFileName(JFreeChart chart,HttpSession session,PrintWriter pw){
String fileName = null;
ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
try {
fileName = ServletUtilities.saveChartAsPNG(chart, 700, 450, info, session); //生成臨時文件
ChartUtilities.writeImageMap(pw, fileName, info, false);
pw.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
// String graphURL = request.getContextPath()+ "/servlet/DisplayChart?fileName=" + fileName;
// request.setAttribute("fileName",fileName);
// request.setAttribute("graphURL",graphURL);
return fileName;//返回生成圖片的文件名 ,傳入jsp
}
}
jsp中使用:
代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.chart.test.*"%>
<%@ page import = "java.io.PrintWriter" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
ChartTest chart=new ChartTest();
String fileName=chart.getFileName(chart.getChart(), session, new PrintWriter(out));
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + fileName;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'JspChart.jsp' starting page</title>
</head>
<body>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="<%= fileName %>">
</body>
</html>