首先要有jfreechart.jar和jcommon-1.0.12.jar兩個(gè)包然后在web.xml配置
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)高碑店免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
servlet
servlet-nameDisplayChart/servlet-name
servlet-classorg.jfree.chart.servlet.DisplayChart/servlet-class
/servlet
servlet-mapping
servlet-nameDisplayChart/servlet-name
url-pattern/DisplayChart/url-pattern
/servlet-mapping
最后是jsp代碼:
%@ page contentType="text/html;charset=GBK"%
%@ page import="org.jfree.chart.ChartFactory,
org.jfree.chart.JFreeChart,
org.jfree.chart.plot.PlotOrientation,
org.jfree.chart.servlet.ServletUtilities,
org.jfree.data.category.CategoryDataset,
org.jfree.data.general.DatasetUtilities"%
%
double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};
String[] rowKeys = {"豬肉", "牛肉","雞肉", "魚肉"};
String[] columnKeys = {""};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("廣州肉類銷量統(tǒng)計(jì)圖", "肉類",
"銷量",
dataset,
PlotOrientation.VERTICAL,
true,
false,
false);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%
img src="%= graphURL %"width=500 height=300 border=0 usemap="#%= filename %"
如果是柱狀圖之類的表示 是在 excel中生成的 那么你可以考慮做個(gè)excel模版引用數(shù)據(jù)利用excel內(nèi)的機(jī)制自動(dòng)生成想要的圖,而數(shù)據(jù)可以用java利用poi的相關(guān)類 往模版里面寫數(shù)據(jù)。就知道這么多 就給你說這么多吧
JFreeChart是JAVA平臺(tái)上的一個(gè)開放的圖表繪制類庫。它完全使用JAVA語言編寫,是為applications, applets, servlets 以及JSP等使用所設(shè)計(jì)。JFreeChart可生成餅圖(pie charts)、柱狀圖(bar charts)、散點(diǎn)圖(scatter plots)、時(shí)序圖(time series)、甘特圖(Gantt charts)等等多種圖表,并且可以產(chǎn)生PNG和JPEG格式的輸出,還可以與PDF和EXCEL關(guān)聯(lián)。
JFreeChart的主頁地址為:
在這里可以找到最新版本的JFreeChart的相關(guān)信息,如說明文檔、下載連接以及示例圖表等。
JFreeChart目前是最好的java圖形解決方案,基本能夠解決目前的圖形方面的需求。
IBM文檔:
Javaeye社區(qū):
ChartDirector除了一個(gè)英文件的幫助以外,也沒有再提供Java DOC形式的文檔,為了方便,寫以下一個(gè)例子說明使用ChartDirector生成柱狀圖的方法.jsp方式實(shí)質(zhì)與JAVA方式?jīng)]有區(qū)別,這里是我從JSP中取的代碼(JSP改起來方便,不過手動(dòng))
代碼如下:
%@ page language="java" contentType="text/Html; charset=UTF-8"
pageEncoding="UTF-8" import="ChartDirector.*;"%
%
request.setCharacterEncoding("UTF-8");
//以兩個(gè)系列數(shù)據(jù)為例
double[] data = {185, 156, 179.5, 211, 123};
double[] data1 = {55, 76, 34.5, 88, 43};
//數(shù)據(jù)列名
String[] labels = {"一月", "二月", "三月", "四月", "五月"};
//生成圖片大小 250 x 250
XYChart c = new XYChart(550, 350);
//圖標(biāo)題
c.addTitle("第一個(gè)圖","",15);
//支持中文
c.setDefaultFonts("SIMSUN.TTC","simhei.ttf");
//圖表在圖片中的定位及區(qū)域大小
c.setPlotArea(30, 40, 400, 250);
//=========================
//加入單個(gè)數(shù)據(jù)
//BarLayer layer = c.addBarLayer(data,0xff3456,"我的測試");
//=========================
//加入多個(gè)BAR數(shù)據(jù)(多個(gè)datasets)
BarLayer layer = c.addBarLayer2(Chart.Side, 3);
layer.addDataSet(data, 0xff8080, "我測試1");
layer.addDataSet(data1, 0x008080, "你也測2");
//3d化
layer.set3D();
//設(shè)置BAR邊框形式
layer.setBarShape(0);
//bar寬度
layer.setBarWidth(50);
//設(shè)置BAR邊框顏色
//layer.setBorderColor(0xff9999);
//圖例形式
layer.setLegend(1);
//每個(gè)BAR頂部加入數(shù)據(jù)顯示
layer.setAggregateLabelStyle();
//設(shè)置BAR底部的名稱顯示
TextBox t = c.xAxis().setLabels(labels);
//名稱文字大小
t.setFontSize(9);
//加圖例
//LegendBox legend = c.addLegend(260, 120,true);
//legend.addKey("錢財(cái)",0xff8080);
//圖例位置
c.addLegend(450, 120,true);
//output the chart
String chart1URL = c.makeSession(request, "chart1");
//include tool tip for the chart
String imageMap1 = c.getHTMLImageMap("#", "", "title='{xLabel}: US${value}K'");
%!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
title圖表測試/title
/head
body
h1中文/h1
hr color="#000080"
br
img src='%=response.encodeURL("getchart.jsp?"+chart1URL)%'
usemap="#map1" border="0"
map name="map1"%=imageMap1%/map
/body
/html
資料引用:
用jfreechart
jfreechart繪制柱狀圖
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/*
* 繪制柱狀圖
*你亮哥
* */
public class BarChart3DDemo
{
public static void main(String[] args)
{
try
{
//設(shè)置主題
ChartFactory.setChartTheme(Theme.getTheme());
//構(gòu)造數(shù)據(jù)
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "JAVA","1");
dataset.addValue(200, "js","1");
dataset.addValue(200, "C++", "2");
dataset.addValue(300, "C", "3");
dataset.addValue(400, "HTML", "4");
dataset.addValue(400, "CSS", "5");
/*
* public static JFreeChart createBarChart3D(
* java.lang.String title, 設(shè)置圖表的標(biāo)題
* java.lang.String categoryAxisLabel, 設(shè)置分類軸的標(biāo)示
* java.lang.String valueAxisLabel, 設(shè)置值軸的標(biāo)示
* CategoryDataset dataset, 設(shè)置數(shù)據(jù)
* PlotOrientation orientation, 設(shè)置圖表的方向
* boolean legend, 設(shè)置是否顯示圖例
* boolean tooltips,設(shè)置是否生成熱點(diǎn)工具
* boolean urls) 設(shè)置是否顯示url
*/
JFreeChart chart = ChartFactory.createBarChart3D("編程語言統(tǒng)計(jì)", "語言",
"學(xué)習(xí)人數(shù)", dataset, PlotOrientation.VERTICAL, true, false,
false);
//保存圖表
ChartUtilities.saveChartAsPNG(new File("E:/chart/BarChart3D.png"), chart, 800, 500);
System.out.println("繪圖完成");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
===================================================================================
//一條線 有點(diǎn) 有數(shù)
package Test;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisSpace;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
public class try123 {
public static void main(String[] args){
//首先構(gòu)造數(shù)據(jù)
TimeSeries timeSeries = new TimeSeries("BMI", Month.class);
// 時(shí)間曲線數(shù)據(jù)集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
// 構(gòu)造數(shù)據(jù)集合
timeSeries.add(new Month(1, 2009), 45);
timeSeries.add(new Month(2, 2009), 46);
timeSeries.add(new Month(3, 2009), 1);
timeSeries.add(new Month(4, 2009), 500);
timeSeries.add(new Month(5, 2009), 43);
timeSeries.add(new Month(6, 2009), 324);
timeSeries.add(new Month(7, 2009), 632);
timeSeries.add(new Month(8, 2009), 34);
timeSeries.add(new Month(9, 2009), 12);
timeSeries.add(new Month(10, 2009), 543);
timeSeries.add(new Month(11, 2009), 32);
timeSeries.add(new Month(12, 2009), 225);
lineDataset.addSeries(timeSeries);
JFreeChart chart = ChartFactory.createTimeSeriesChart("", "date", "bmi", lineDataset, true, true, true);
//增加標(biāo)題
chart.setTitle(new TextTitle("XXXBMI指數(shù)", new Font("隸書", Font.ITALIC, 15)));
chart.setAntiAlias(true);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(10,10,10,10));//圖片區(qū)與坐標(biāo)軸的距離
plot.setOutlinePaint(Color.PINK);
plot.setInsets(new RectangleInsets(15,15,15,15));//坐標(biāo)軸與最外延的距離
// plot.setOrientation(PlotOrientation.HORIZONTAL);//圖形的方向,包括坐標(biāo)軸。
AxisSpace as = new AxisSpace();
as.setLeft(25);
as.setRight(25);
plot.setFixedRangeAxisSpace(as);
chart.setPadding(new RectangleInsets(5,5,5,5));
chart.setNotify(true);
// 設(shè)置曲線是否顯示數(shù)據(jù)點(diǎn)
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
// 設(shè)置曲線顯示各數(shù)據(jù)點(diǎn)的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE10, TextAnchor.BASELINE_LEFT));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setRenderer(xyitem);
//顯示
ChartFrame frame = new ChartFrame("try1", chart);
frame.pack();
frame.setVisible(true);
}
}