我的Blog我做主^_^

走向一条通往JAVA的不归路...

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 68 文章 :: 77 评论 :: 0 Trackbacks

一、饼图
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:directive.page import="org.jfree.data.category.CategoryDataset"/>
<jsp:directive.page import="org.jfree.data.general.DatasetUtilities"/>
<jsp:directive.page import="org.jfree.chart.JFreeChart"/>
<jsp:directive.page import="org.jfree.chart.ChartFactory"/>
<jsp:directive.page import="org.jfree.chart.plot.CategoryPlot"/>
<jsp:directive.page import="java.awt.Color"/>
<jsp:directive.page import="org.jfree.chart.axis.CategoryAxis"/>
<jsp:directive.page import="java.awt.Font"/>
<jsp:directive.page import="java.io.PipedReader"/>
<jsp:directive.page import="org.jfree.chart.servlet.ServletUtilities"/>
<jsp:directive.page import="org.jfree.data.general.PieDataset"/>
<jsp:directive.page import="org.jfree.data.general.DefaultPieDataset"/>
<%

String[] rowKeys= null;//柱数据
String[] columnKeys=null;//刻度数据

rowKeys = new String[]{"219.146.1.196","219.146.10.105","219.146.10.116","219.146.10.73"};
columnKeys = new String[]{"2007-02-03","2007-02-04"};

double[][] data = new double[rowKeys.length][columnKeys.length];
data = new double[][]{
  {1022.0,856.0},{676.0,13563.0},{12827.0,10576.0},{0.0,100.0}  
  };
DefaultPieDataset dataset = new DefaultPieDataset();
for(int m=0;m<rowKeys.length;m++){
    dataset.setValue(rowKeys[m],data[m][0]);
}
JFreeChart chart = ChartFactory.createPieChart3D(
 "this is title......",
 dataset,
 true,
 false,
 false
);
 
chart.setBackgroundPaint(Color.CYAN);
chart.setBorderPaint(Color.RED);
chart.setBorderVisible(true);

String filename = ServletUtilities.saveChartAsPNG(chart, 850, 550, null, session);
String graphURL = request.getContextPath() + "/xx?filename=" + filename;
%>
<br>
<link href="css/default.css" rel="stylesheet" type="text/css">
<center>
<img src="<%= graphURL %>" width=850 height=550 border=0 usemap="#<%= filename %>">


二、柱图
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:directive.page import="org.jfree.data.general.DatasetUtilities"/>
<jsp:directive.page import="org.jfree.chart.JFreeChart"/>
<jsp:directive.page import="org.jfree.chart.ChartFactory"/>
<jsp:directive.page import="org.jfree.chart.plot.PlotOrientation"/>
<jsp:directive.page import="java.awt.Color"/>
<jsp:directive.page import="org.jfree.chart.plot.CategoryPlot"/>
<jsp:directive.page import="org.jfree.chart.axis.CategoryAxis"/>
<jsp:directive.page import="org.jfree.chart.axis.CategoryLabelPositions"/>
<jsp:directive.page import="org.jfree.chart.axis.NumberAxis"/>
<jsp:directive.page import="org.jfree.chart.servlet.ServletUtilities"/>
<jsp:directive.page import="org.jfree.data.category.CategoryDataset"/>
<jsp:directive.page import="org.jfree.chart.renderer.category.BarRenderer3D"/>
<jsp:directive.page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator"/>
<jsp:directive.page import="org.jfree.chart.axis.AxisLocation"/>
<jsp:directive.page import="java.text.DecimalFormat"/>
<%
String[] rowInfo = new String[]{"one","two"};
String[] colInfo = new String[]{"first","second"};
double[][] dataInfo = new double[2][2];
dataInfo = new double[][]{
  {1022.0,856.0},{676.0,13563.0}  
  };
CategoryDataset data = DatasetUtilities.createCategoryDataset(rowInfo,colInfo,dataInfo);
JFreeChart chart = ChartFactory.createBarChart3D("this is test......",
                  "row",
                  "col",
                  data,
                  PlotOrientation.VERTICAL,
                  true,false,false);
chart.setAntiAlias(true);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderPaint(Color.BLACK);
chart.setBorderVisible(true);

//图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainGridlinePaint(Color.RED); //横坐标网格线白色
plot.setDomainGridlinesVisible(true); //可见
plot.setBackgroundPaint(Color.CYAN);

//用于处理图表的两个轴:纵轴和横轴
CategoryAxis axis = plot.getDomainAxis();
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
axis.setAxisLinePaint(Color.RED);
//axis.setLabel("asdfasdfa");
//axis.setUpperMargin(5);
axis.setTickMarksVisible(true);
axis.setAxisLineVisible(true);


NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis();
numberaxis.setAutoTickUnitSelection(true);
numberaxis.setAutoRangeIncludesZero(false);

BarRenderer3D renderer = new BarRenderer3D();

renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色
renderer.setWallPaint(Color.GREEN);
//设置每种柱的颜色
//renderer.setSeriesPaint(0, new Color(0, 0, 255));
//renderer.setSeriesPaint(1, new Color(0, 100, 255));
//设置平行柱之间距离
renderer.setItemMargin(0.02);
//显示每个柱的数值,并修改该数值的字体属性
//renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);

//设置柱的透明度
plot.setForegroundAlpha(0.8f);
//设置地区、销量的显示位置
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

String filename = ServletUtilities.saveChartAsPNG(chart, 850, 550, null, session);
String graphURL = request.getContextPath() + "/xx?filename=" + filename;
 %>

<center>
<img src="<%= graphURL %>" width=850 height=550 border=0 usemap="#<%= filename %>">

三、折线图
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color"%>
<%@ page import="myjava.TableData"%>
<%@ page import = "org.jfree.chart.ChartFactory,
                  org.jfree.chart.ChartPanel,
                  org.jfree.chart.JFreeChart,
                  org.jfree.chart.DefaultOldLegend,
                  org.jfree.chart.axis.NumberAxis,
  org.jfree.chart.plot.CategoryPlot,
  org.jfree.chart.plot.PlotOrientation,
  org.jfree.chart.renderer.category.LineAndShapeRenderer,
  org.jfree.data.category.CategoryDataset,
  org.jfree.data.category.DefaultCategoryDataset,
  org.jfree.chart.servlet.ServletUtilities
"%>
<%@ page import="org.jfree.chart.axis.CategoryAxis"%>
<%@ page import="org.jfree.chart.axis.CategoryLabelPositions"%>
<%@ page import="org.jfree.data.general.DatasetUtilities"%>
<jsp:directive.page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator"/>

<%

String[] rowKeys= null;//柱数据
String[] columnKeys=null;//刻度数据

rowKeys = new String[]{"219.146.1.196","219.146.10.105","219.146.10.116","219.146.10.73"};
columnKeys = new String[]{"2007-02-03","2007-02-04"};

double[][] data = new double[rowKeys.length][columnKeys.length];
data = new double[][]{
  {1022.0,856.0},{676.0,13563.0},{12827.0,10576.0},{0.0,100.0}  
  };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createLineChart(
           "这是测试标题...",
            "时间",
            "访问量",                 // range axis label
           dataset,                   // data
           PlotOrientation.VERTICAL,  // orientation
           true,                      // include legend
           true,                      // tooltips
           false                      // urls
       );
chart.setBackgroundPaint(Color.WHITE);  //-----------------------------背景色
chart.setBorderVisible(true);  //--------------------------------------设置边框是否可见
chart.setBorderPaint(Color.BLUE);  //-----------------------------------设置边框颜色setBorderVisible()必须为true

CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.CYAN);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        plot.setDomainAxis(domainAxis);
       // customise the range axis... 设置统计图中只显示整数
       NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
       rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
       rangeAxis.setAutoRangeIncludesZero(true);
       rangeAxis.setUpperMargin(0.20);
       rangeAxis.setLabelAngle(Math.PI / 2.0); 
         
       LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();  
       renderer.setItemLabelsVisible(true);
       renderer.setShapesVisible(true);//series 点(即数据点)可见
       renderer.setSeriesPaint(0, new Color(0, 0, 255));
       renderer.setSeriesPaint(1, new Color(255, 0, 255));
       renderer.setSeriesPaint(2, new Color(0, 255, 255));
       renderer.setSeriesPaint(3, new Color(0,125,0));
       renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setItemLabelsVisible(true);
     
String filename = ServletUtilities.saveChartAsPNG(chart, 850, 550, null, session);
String graphURL = request.getContextPath() + "/xx?filename=" + filename;
%>
<br>
<link href="css/default.css" rel="stylesheet" type="text/css">
<center>
<img src="<%= graphURL %>" width=850 height=550 border=0 usemap="#<%= filename %>">

 



posted on 2007-02-07 10:44 java_蝈蝈 阅读(4911) 评论(2)  编辑  收藏

评论

# re: jfreechart例子 2007-06-12 19:55 求助
系统:windows 2000 server
strust+spring+hibernate
Tomcat:5.02.8 (5.5.9)
JDK:1.5
jfreechart-1.0.5
jcommon-1.0.10

信息: validateJarFile(C:\Tomcat 5.0\webapps\foods\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
17:48:27,609 ERROR ContextLoader:200 - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from ServletContext resource [/WEB-INF/ApplicationContext-hibernate.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@1558dc] does not support XML Schema. Are you running on Java 1.4 or below with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
Caused by:
javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@1558dc] does not support XML Schema. Are you running on Java 1.4 or below with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/ApplicationContext-hibernate.xml]

这是什么错啊...如果不使用jfreechart就不会出错...帮忙看看  回复  更多评论
  

# re: jfreechart例子 2008-12-31 12:48 cola.zhang
我也遇到过这样的问题,你的jar包中是否有一个gnujaxp.jar 啊?如果有的话,把这个包去掉就可以了!  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: