java类中使用Jfreechart的例子
对于Jfreechart是不是很多小伙伴们都不知道怎么使用它呢?其实使用Jfreechart的方法是非常简单的,那么下面我们就一起去看看java类中使用Jfreechart的例子介绍。
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>
以上就是小编为大家介绍java类中使用Jfreechart的例子,其实大家在掌握了其方法后会发现并没有我们想象中那么困难。
上一篇:Java如何将数字转成英文
下一篇:java代码如何保留小数点的位数