你需要看一下org.jfree.chart.encoders.KeypointPNGEncoderAdapter这个代码

解决方案 »

  1.   

    程序的相关代码如下:
    请大家帮忙!这是我用java写的生成chart的类:
    package com.huiton.SCM.logis.inv;import java.awt.*;
    import java.util.TimeZone;
    import org.jfree.chart.*;
    import org.jfree.chart.axis.PeriodAxis;
    import org.jfree.chart.axis.PeriodAxisLabelInfo;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
    import org.jfree.data.time.*;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.*;
    import org.jfree.chart.entity.StandardEntityCollection;
    import org.jfree.chart.servlet.ServletUtilities;
    import javax.servlet.http.HttpSession;
    import java.io.PrintWriter;
    import java.awt.Font;
    import org.jfree.chart.title.TextTitle;public class PeriodXYChart {
      protected  String filename="";
       protected  String title="";
      protected  int chartHight=0;
      protected  int chartWidth=0;  public PeriodXYChart(String title,int chartWidth,int chartHight) {
        this.chartHight=chartHight;
        this.chartWidth=chartWidth;
        this.title=title;
      }
       public String generateXYChart(HttpSession session, PrintWriter pw,XYDataset xydataset)
    {
      try
      {
        JFreeChart jfreechart=PeriodXYChart.createChart(title,xydataset);
         System.out.println("--------------- create jfreechart---------");
        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        filename = ServletUtilities.saveChartAsPNG(jfreechart, chartWidth, chartHight, info, session);
        //把image map 写入到 PrintWriter
        ChartUtilities.writeImageMap(pw, filename, info);
                pw.flush();
      }
      catch(Exception ex)
      {
        System.out.println("error:" + ex.getMessage());  }
      return  filename;
    }   static JFreeChart createChart(String title,XYDataset xydataset)
        {
            Font font;
            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "日期", "零件用量", xydataset, true, true, false);
            jfreechart.setBackgroundPaint(Color.white);
            jfreechart.setTitle(new TextTitle(title, new Font("黑体", Font.BOLD, 17), Color.black));        StandardLegend standardlegend = (StandardLegend)jfreechart.getLegend();
            standardlegend.setDisplaySeriesShapes(true);
            XYPlot xyplot = jfreechart.getXYPlot();
            font = new Font("黑体", Font.CENTER_BASELINE, 20);
            xyplot.setBackgroundPaint(Color.white);
            xyplot.setDomainGridlinePaint(Color.lightGray);
            xyplot.setRangeGridlinePaint(Color.lightGray);
            xyplot.setAxisOffset(new Spacer(1, 5D, 5D, 5D, 5D));
            xyplot.setDomainCrosshairVisible(true);
            xyplot.setRangeCrosshairVisible(true);
            org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
            if(xyitemrenderer instanceof StandardXYItemRenderer)
            {
                StandardXYItemRenderer standardxyitemrenderer = (StandardXYItemRenderer)xyitemrenderer;
                standardxyitemrenderer.setPlotShapes(true);
                standardxyitemrenderer.setShapesFilled(true);
                standardxyitemrenderer.setItemLabelsVisible(true);
            }
            PeriodAxis periodaxis = new PeriodAxis("日期");
            periodaxis.setTimeZone(TimeZone.getDefault());
            periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Day.class);
            periodaxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
            PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[3];
            aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, "d");
            aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class, "MMM", new Spacer(1, 2D, 2D, 2D, 2D), new Font("黑体", 1, 10), Color.black, false, null, null);
            aperiodaxislabelinfo[2] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class, "yyyy");
            periodaxis.setLabelInfo(aperiodaxislabelinfo);
            xyplot.setDomainAxis(periodaxis);
            return jfreechart;
        }
    }
    下面在jsp网页中调用该类画图:
    其中,timeseriescollection在另外一个文件中生成,传到jsp页面
    下面是include进去的画图页面<%@ page contentType="text/html;charset=GBK"%>
    <%@page import="com.huiton.SCM.pub.util.functions.*"%>
    <%@page import="com.huiton.SCM.logis.inv.PeriodXYChart"%>
    <%@ page contentType="text/html;charset=gb2312"%><%@ page import = "java.io.PrintWriter" %>
    <%@ page import = "java.text.SimpleDateFormat" %>
    <%@ page import = "java.text.ParseException" %>
           <% 
          
           PeriodXYChart XYChart=new PeriodXYChart("零件用量统计图",500,300);
           String filename=XYChart.generateXYChart(session,new PrintWriter(out),timeseriescollection);
            //System.out.println("22222222222222222222222");
           String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;    
           
    %>
    <script language="javascript">function closeMe()
    {
    self.close();
    }</script><table>
    <tr>
    <td>
    <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> 
           </body>
           </html>
    </td>
    </tr><tr></tr></table>