java jfreechart例子_java类中使用Jfreechart的简单实例

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值