`

JfreeChart作好的折线+曲线图!

    博客分类:
  • JAVA
阅读更多

折线图 line.jsp
XY曲线图 xy.jsp
时间曲线图 timeserver.jsp
其他曲线图 MultipleAxisDemo.jsp


折线图 line.jsp :



<%@ page contentType="text/html;charset=GBK"%>
<%@ page import = "org.jfree.chart.ChartFactory,
                  org.jfree.chart.ChartPanel,
                  org.jfree.chart.JFreeChart,
                  org.jfree.chart.StandardLegend,
                  org.jfree.chart.axis.NumberAxis,
  org.jfree.chart.plot.CategoryPlot,
  org.jfree.chart.plot.PlotOrientation,
  org.jfree.chart.renderer.LineAndShapeRenderer,
  org.jfree.data.CategoryDataset,
  org.jfree.data.DefaultCategoryDataset,
  org.jfree.chart.servlet.ServletUtilities
"%>
<%
       
       // row keys...
       String series1 = "First";
       String series2 = "Second";
       String series3 = "Third";

       // column keys...
       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";
       String type8 = "Type 8";

       // create the dataset...
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       dataset.addValue(1.0, series1, type1);
       dataset.addValue(4.0, series1, type2);
       dataset.addValue(3.0, series1, type3);
       dataset.addValue(5.0, series1, type4);
       dataset.addValue(5.0, series1, type5);
       dataset.addValue(7.0, series1, type6);
       dataset.addValue(7.0, series1, type7);
       dataset.addValue(8.0, series1, type8);

       dataset.addValue(5.0, series2, type1);
       dataset.addValue(7.0, series2, type2);
       dataset.addValue(6.0, series2, type3);
       dataset.addValue(8.0, series2, type4);
       dataset.addValue(4.0, series2, type5);
       dataset.addValue(4.0, series2, type6);
       dataset.addValue(2.0, series2, type7);
       dataset.addValue(1.0, series2, type8);

       dataset.addValue(4.0, series3, type1);
       dataset.addValue(3.0, series3, type2);
       dataset.addValue(2.0, series3, type3);
       dataset.addValue(3.0, series3, type4);
       dataset.addValue(6.0, series3, type5);
       dataset.addValue(3.0, series3, type6);
       dataset.addValue(4.0, series3, type7);
       dataset.addValue(3.0, series3, type8);   
   
         
       // create the chart...
       JFreeChart chart = ChartFactory.createLineChart(
           "血压水平分布图",       // chart title
           "Type",                    // domain axis label
           "Value",                   // range axis label
           dataset,                   // data
           PlotOrientation.VERTICAL,  // orientation
           true,                      // include legend
           true,                      // tooltips
           false                      // urls
       );

       // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
       StandardLegend legend = (StandardLegend) chart.getLegend();
       legend.setDisplaySeriesShapes(true);
       legend.setShapeScaleX(1.5);
       legend.setShapeScaleY(1.5);
       legend.setDisplaySeriesLines(true);   
       CategoryPlot plot = chart.getCategoryPlot();
       // 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);          
     
 
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 %>">



XY曲线图 xy.jsp :



<%@ page contentType="text/html;charset=GBK"%>
<%@ page import =" java.awt.*"%>
<%@ page import = " java.awt.geom.Ellipse2D"%>
<%@ page import = " java.io.PrintWriter"%>
<%@ page import = " java.text.SimpleDateFormat"%>
<%@ page import = " java.util.Date"%>
<%@ page import = " java.util.ArrayList"%>
<%@ page import = " java.util.Iterator"%>
<%@ page import = " java.util.Locale"%>
<%@ page import = " java.text.NumberFormat"%>
<%@ page import = " javax.servlet.http.HttpSession"%>
<%@ page import = " org.jfree.data.*"%>
<%@ page import = " org.jfree.chart.*"%>
<%@ page import = " org.jfree.chart.axis.*"%>
<%@ page import = " org.jfree.chart.renderer.BarRenderer"%>
<%@ page import = " org.jfree.chart.renderer.StandardXYItemRenderer"%>
<%@ page import = " org.jfree.chart.renderer.StackedXYAreaRenderer"%>
<%@ page import = " org.jfree.chart.renderer.XYAreaRenderer"%>
<%@ page import = " org.jfree.chart.plot.*"%>
<%@ page import = " org.jfree.chart.entity.*"%>
<%@ page import = " org.jfree.chart.labels.*"%>
<%@ page import = " org.jfree.chart.urls.*"%>
<%@ page import ="org.jfree.chart.servlet.*"%>
<%@ page import = "java.io.PrintWriter"%>
<%@ page import = "java.util.ArrayList"%>
<%@ page import = "java.util.Iterator,org.jfree.data.time.*,java.awt.Font"%>
<%
XYSeries dataSeries = new XYSeries("增长");     

for (int i = 0; i <= 10; i++)
{
int x=(int) (100*Math.random());
int y=(int) (100*Math.random());
dataSeries.add(x,y);
}



XYSeries dataSeries1 = new XYSeries("趋势");     

for (int j = 0; j <= 100; j++)
{
int x=(int) (100*Math.random());
int y=(int) (100*Math.random());
dataSeries1.add(j,40);
}


XYSeriesCollection xyDataset = new XYSeriesCollection();

xyDataset.addSeries(dataSeries);
xyDataset.addSeries(dataSeries1);


String filename = "";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,sdf,NumberFormat.getInstance());

TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(sdf, "pie_chart.jsp", "series", "hitDate");
//  Create the chart object

ValueAxis xAxis = new NumberAxis("X轴坐标");
//x轴坐标
ValueAxis yAxis = new NumberAxis("Y轴坐标");
//y轴坐标
StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES ,ttg, urlg);
renderer.setShapesFilled(true);
            
Font font=new Font("黑体",Font.TRUETYPE_FONT, 12);
XYPlot plot = new XYPlot(xyDataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart("测试曲线图", font, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);            

//  Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);    
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;%>
<html>
<head>
</head>
<body>
<table border=0>
<tr>
<td>
   <br>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">

</td>
</tr>
</table>
</body>
</html>



时间曲线图 timeserver.jsp :



<%@ page contentType="text/html;charset=GBK"%>
<%@ page import = "org.jfree.chart.ChartFactory,
                  org.jfree.chart.ChartPanel,
                  org.jfree.chart.JFreeChart,
  org.jfree.chart.servlet.ServletUtilities,
  org.jfree.chart.*,
  org.jfree.chart.StandardLegend,
  org.jfree.chart.axis.DateAxis,
  org.jfree.chart.plot.XYPlot,
  org.jfree.chart.renderer.StandardXYItemRenderer,
  org.jfree.chart.renderer.XYItemRenderer,
  org.jfree.data.XYDataset,
  org.jfree.data.time.Month,
  org.jfree.data.time.TimeSeries,
  org.jfree.data.time.TimeSeriesCollection
"%>
<%

       TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
       s1.add(new Month(2, 2001), 181.8);
       s1.add(new Month(3, 2001), 167.3);
       s1.add(new Month(4, 2001), 153.8);
       s1.add(new Month(5, 2001), 167.6);
       s1.add(new Month(6, 2001), 158.8);
       s1.add(new Month(7, 2001), 148.3);
       s1.add(new Month(8, 2001), 153.9);
       s1.add(new Month(9, 2001), 142.7);
       s1.add(new Month(10, 2001), 123.2);
       s1.add(new Month(11, 2001), 131.8);
       s1.add(new Month(12, 2001), 139.6);
       s1.add(new Month(1, 2002), 142.9);
       s1.add(new Month(2, 2002), 138.7);
       s1.add(new Month(3, 2002), 137.3);
       s1.add(new Month(4, 2002), 143.9);
       s1.add(new Month(5, 2002), 139.8);
       s1.add(new Month(6, 2002), 137.0);
       s1.add(new Month(7, 2002), 132.8);

       TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
       s2.add(new Month(2, 2001), 129.6);
       s2.add(new Month(3, 2001), 123.2);
       s2.add(new Month(4, 2001), 117.2);
       s2.add(new Month(5, 2001), 124.1);
       s2.add(new Month(6, 2001), 122.6);
       s2.add(new Month(7, 2001), 119.2);
       s2.add(new Month(8, 2001), 116.5);
       s2.add(new Month(9, 2001), 112.7);
       s2.add(new Month(10, 2001), 101.5);
       s2.add(new Month(11, 2001), 106.1);
       s2.add(new Month(12, 2001), 110.3);
       s2.add(new Month(1, 2002), 111.7);
       s2.add(new Month(2, 2002), 111.0);
       s2.add(new Month(3, 2002), 109.6);
       s2.add(new Month(4, 2002), 113.2);
       s2.add(new Month(5, 2002), 111.6);
       s2.add(new Month(6, 2002), 108.8);
       s2.add(new Month(7, 2002), 101.6);

       TimeSeriesCollection dataset = new TimeSeriesCollection();
       dataset.addSeries(s1);
       dataset.addSeries(s2);

       dataset.setDomainIsPointsInTime(true);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
           "Legal & General Unit Trust Prices",
           "Date", "Price Per Unit",
           dataset,
           true,
           true,
           false
       );

       //chart.setBackgroundPaint(Color.white);

       StandardLegend sl = (StandardLegend) chart.getLegend();
       sl.setDisplaySeriesShapes(true);

       XYPlot plot = chart.getXYPlot();
      // plot.setBackgroundPaint(Color.lightGray);
       //plot.setDomainGridlinePaint(Color.white);
       //plot.setRangeGridlinePaint(Color.white);
      //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
       plot.setDomainCrosshairVisible(true);
       plot.setRangeCrosshairVisible(true);
       
       XYItemRenderer renderer = plot.getRenderer();
       if (renderer instanceof StandardXYItemRenderer) {
           StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
           rr.setPlotShapes(true);
           rr.setShapesFilled(true);
       }
       
       DateAxis axis = (DateAxis) plot.getDomainAxis();
       //axis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy"));

 
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 %>">



其他曲线图 MultipleAxisDemo.jsp :


<%@ page contentType="text/html;charset=GBK"%>
<%@ page import = "org.jfree.chart.ChartFactory,
                  org.jfree.chart.ChartPanel,
                  org.jfree.chart.JFreeChart,
  org.jfree.chart.servlet.ServletUtilities,
  org.jfree.chart.axis.AxisLocation,
  org.jfree.chart.axis.NumberAxis,
  org.jfree.chart.plot.PlotOrientation,
  org.jfree.chart.plot.XYPlot,
  org.jfree.chart.renderer.StandardXYItemRenderer,
  org.jfree.chart.title.TextTitle,
  org.jfree.data.XYDataset,
  org.jfree.data.time.Minute,
  org.jfree.data.time.RegularTimePeriod,
  org.jfree.data.time.TimeSeries,
  org.jfree.data.time.TimeSeriesCollection

  
"%>
<%
String name="Series 1";
double base=100.0;
RegularTimePeriod start=new Minute();
int count=200;
 TimeSeries series = new TimeSeries(name, start.getClass());
       RegularTimePeriod period = start;
       double value = base;
       for (int i = 0; i < count; i++) {
           series.add(period, value);    
           period = period.next();
           value = value * (1 + (Math.random() - 0.495) / 10.0);
       }

       TimeSeriesCollection dataset = new TimeSeriesCollection();
       dataset.addSeries(series);


       
       JFreeChart chart = ChartFactory.createTimeSeriesChart(
           "Multiple Axis Demo 1",
           "Time of Day",
           "Primary Range Axis",
           dataset,
           true,
           true,
           false
       );

       //chart.setBackgroundPaint(Color.white);
       chart.addSubtitle(new TextTitle("Four datasets and four range axes."));  
       XYPlot plot = chart.getXYPlot();
       plot.setOrientation(PlotOrientation.VERTICAL);
       //plot.setBackgroundPaint(Color.lightGray);
       //plot.setDomainGridlinePaint(Color.white);
       //plot.setRangeGridlinePaint(Color.white);
       
       //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
       
       StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
       //renderer.setPaint(Color.black);
      
      /* // AXIS 2
       NumberAxis axis2 = new NumberAxis("Range Axis 2");
       axis2.setAutoRangeIncludesZero(false);
       //axis2.setLabelPaint(Color.red);
       //axis2.setTickLabelPaint(Color.red);
       //plot.setSecondaryRangeAxis(0, axis2);
       //plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);

       XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170);
       plot.setSecondaryDataset(0, dataset2);
       plot.mapSecondaryDatasetToRangeAxis(0, new Integer(0));
       plot.setSecondaryRenderer(0, new StandardXYItemRenderer());
      // plot.getSecondaryRenderer(0).setSeriesPaint(0, Color.red);
       
       // AXIS 3
       NumberAxis axis3 = new NumberAxis("Range Axis 3");
       //axis3.setLabelPaint(Color.blue);
       //axis3.setTickLabelPaint(Color.blue);
       //plot.setSecondaryRangeAxis(1, axis3);

       XYDataset dataset3 = createDataset("Series 3", 10000.0, new Minute(), 170);
       plot.setSecondaryDataset(1, dataset3);
       plot.mapSecondaryDatasetToRangeAxis(1, new Integer(1));
       
       plot.setSecondaryRenderer(1, new StandardXYItemRenderer());
      //plot.getSecondaryRenderer(1).setSeriesPaint(0, Color.blue);

       // AXIS 4        
       NumberAxis axis4 = new NumberAxis("Range Axis 4");
       axis4.setLabelPaint(Color.green);
       axis4.setTickLabelPaint(Color.green);
      // plot.setSecondaryRangeAxis(2, axis4);
       
       XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200);
       plot.setSecondaryDataset(2, dataset4);
       plot.mapSecondaryDatasetToRangeAxis(2, new Integer(2));
       
       plot.setSecondaryRenderer(2, new StandardXYItemRenderer());
       //plot.getSecondaryRenderer(2).setSeriesPaint(0, Color.green);      
*/
               


 
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 %>">




注意以上代码,均在:jfreechart-0.9.20.jar、jcommon-0.9.5.jar下调试通过!

web.xml配置servlet代码:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <servlet>
   <servlet-name>DisplayChart</servlet-name>
   <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
 </servlet>
  <servlet-mapping>
       <servlet-name>DisplayChart</servlet-name>
       <url-pattern>/DisplayChart</url-pattern>
   </servlet-mapping>
</web-app>

 

分享到:
评论

相关推荐

    JFreeChart 折线、柱状组合图

    public JFreeChart createChart(String Ytitle, String title, CategoryDataset lineData, CategoryDataset barData) { //参考附件 return chart; } //struts 部分 &lt;!--begin 维优特例 --&gt; ...

    flex +jfreechart生成 饼状图+曲线图+柱状图(完整版)

    Flex是一种基于ActionScript的开源框架,主要用于构建富互联网应用程序(RIA),而JFreeChart则是一个Java库,专门用于生成高质量的图表,包括饼状图、柱状图和曲线图等。 在Flex中,我们通常使用MXML和...

    java+jfreechart+struts 生成曲线图

    java+jfreechart+struts 生成曲线图 java+jfreechart+struts 生成曲线图 java jfreechart 曲线图 java jfreechart 曲线图 java jfreechart 曲线图 谁需要完整项目可以去http://download.csdn.net/source/2665347

    JFreeChart双Y轴折线图实例,可以直接运行

    JFreeChart双Y轴折线图实例,可以直接运行,实例类为LineChartDemo1.JAVA,有注释。 若想在web工程使用只需如下。 String filename = ServletUtilities.saveChartAsPNG(jfreechart, 600, 400, null, session); ...

    jfreechart 堆栈柱状图跟折线图结合拼接

    jfreechart 堆栈柱状图跟折线图结合拼接 jar包请戳http://download.csdn.net/detail/a156435646/7424707

    jfreechart 生成折线图,饼图,柱状图,堆栈柱状图

    JFreeChart 是一个开源的Java库,用于生成高质量的2D图表,如折线图、饼图、柱状图和堆栈柱状图等。它广泛应用于数据分析、报告和应用程序中,提供丰富的自定义选项来满足各种视觉需求。在本项目中,你将找到能够...

    JSP利用JFreeChart生成折线图

    在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的技术,而JFreeChart则是一个强大的Java图表库,能够帮助开发者生成各种复杂的图表,包括折线图。本教程将详细介绍如何在JSP中利用JFreeChart库来...

    jfreechart绘制折线图(平面和3D)

    JFreeChart是一款强大的Java图形库,它允许开发者创建多种类型的图表,包括折线图、柱状图、饼图、散点图等。在本主题中,我们将深入探讨如何使用JFreeChart来绘制平面和3D的折线图。 首先,让我们了解折线图的基本...

    jfreechart 漂亮的折线图

    利用jfreechart绘制的漂亮的折线图,实现双纵坐标,折线点标签还带箭头指示,非常直观,需要提前下载jfreechart.jar文件,否则我的类会报错,也即是要配置运行环境

    jfreechart各类曲线图JSP代码

    JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。本文将通过引领...

    java JXL导入导出Excel源码及jfreechart 生成折线图,饼图

    java JXL导出Excel源码及jfreechart 生成折线图,饼图 java JXL导出Excel源码及jfreechart 生成折线图,饼图 java JXL导出Excel源码及jfreechart 生成折线图,饼图

    jfreechart实例教程+笔记+流程图

    JFreeChart 是一个功能强大的 Java 图表库,它为开发者提供了创建各种高质量图表的能力,包括折线图、柱状图、饼图、散点图、甘特图等。这个教程将帮助你深入理解和使用 JFreeChart,结合学习笔记、流程图和示例代码...

    JFreeChart画折线实例

    在Java编程环境中,JFreeChart是一个非常流行的开源库,它为开发者提供了丰富的图表绘制功能,包括折线图、饼图、柱状图等。在这个"JFreeChart画折线实例"中,我们将深入探讨如何利用JFreeChart库来创建折线图,并将...

    jfreechart折线图demo

    **JFreeChart 折线图 Demo 知识点详解** JFreeChart 是一个用 Java 编写的开源图表库,它提供了丰富的图表类型,包括折线图、柱状图、饼图等,适用于各种数据可视化需求。在这个“jfreechart折线图demo”中,我们将...

    JfreeChart画双Y轴折线图

    在Java编程领域,JFreeChart是一个非常流行的图表库,它允许开发者创建各种复杂的数据可视化图形,包括折线图、柱状图、饼图等。在本篇博客中,"JfreeChart画双Y轴折线图"的主题,将探讨如何使用JFreeChart库来创建...

    jfreechart 折线图 应用

    该程序为main 函数,定义了三个方法,分别是jfreechart三种实现折线图的类型,返回的是一个chart 本程序是把图片保存到本地,当然实际应用中,只要在程序和页面中做相关配置即可以使用。 因为图片带点透明,所以...

    jfreechart绘制的风速风向玫瑰图

    jfreechart是Java中一个流行的图表库,它提供了许多种类的图表,包括柱状图、折线图、饼图、雷达图等。本文主要介绍如何使用jfreechart绘制风速风向玫瑰图。 首先,了解jfreechart的基本结构。jfreechart的核心是...

    jfreechart 柱状图、曲线图、饼图经

    JFreeChart是一款强大的Java图形库,它为开发者提供了丰富的图表类型,包括柱状图、曲线图和饼图等,使得在Java应用中展示数据变得更加直观和美观。在本篇文章中,我们将深入探讨如何利用JFreeChart来创建这些图表,...

    JFreeChart简单实现光滑曲线绘制

    JFreeChart是一个流行的开源图表库,用于生成各种类型的图表,包括折线图、柱状图、饼图等。下面我们将介绍如何使用JFreeChart简单实现光滑曲线绘制。 光滑曲线绘制 光滑曲线绘制是一种常用的图表绘制方式,用于...

Global site tag (gtag.js) - Google Analytics