java類中使用Jfreechart的簡單一實例_java

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.