欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java类中使用Jfreechart的简单实例

程序员文章站 2023-12-17 08:05:04
package com.chart.test;复制代码 代码如下:import java.awt.basicstroke;import java.awt.color;imp...

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>

上一篇:

下一篇: