修改JFreeChart 图片的存放路径

/**

**QQ:252574345

**MSN:lele_love_lomboz@hotmail.com

*/

 

跟踪发现,JFreeChart会把生成的图片,默认放在应用服务器的temp目录下,有的时候我们是不能放在该目录下的,需要改变这个存放路径

发现jfreechart的ServletUtilities类里有

    protected static void createTempDir() {
        String tempDirName = System.getProperty("java.io.tmpdir");
        if (tempDirName == null) {
            throw new RuntimeException("应用服务器目录下不存在temp目录或该目录无法创建");
        }

        // create the temporary directory if it doesn't exist
        File tempDir = new File(tempDirName);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
    }

该方法创建了默认的图片存放路径

在该类的saveChartAsPNG() 和saveChartAsJPEG () 里被调用,产生图形,因此我们的思路就是将ServletUtilities的saveChartAsPNG() 和saveChartAsJPEG () 这2个方法改造成自己定义的方法

修改前源文件如下:

    public static String saveChartAsJPEG(JFreeChart chart, int width,
            int height, ChartRenderingInfo info, HttpSession session)
            throws IOException {

        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");  
        }
        //注意,源文件使用了默认路径
        ServletUtilities.createTempDir();
        String prefix = ServletUtilities.tempFilePrefix;
        if (session == null) {
            prefix = ServletUtilities.tempOneTimeFilePrefix;  
        }
        File tempFile = File.createTempFile(prefix, ".jpeg",
                new File(System.getProperty("java.io.tmpdir")));
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
        if (session != null) {
            ServletUtilities.registerChartForDeletion(tempFile, session);
        }
        return tempFile.getName();

    }

 

修改后如下:

    public static String saveChartAsJPEG(JFreeChart chart, int width,
            int height, ChartRenderingInfo info, HttpSession session)
            throws IOException {

//从application中读取出临时文件目录,我事先已经在系统启动时,创建了目录
     
       File tempDr=(File)session.getServletContext().getAttribute("tempDirectory");

      
      
        if (chart == null) {
            throw new IllegalArgumentException("chart 对象为空");  
        }
       
        UIServletUtilities.createTempDir();
        String prefix = UIServletUtilities.tempFilePrefix;
        if (session == null) {
            prefix = UIServletUtilities.tempOneTimeFilePrefix;  
        }
        File tempFile = File.createTempFile(prefix, ".jpeg", tempDr);
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
        if (session != null) {
         UIServletUtilities.registerChartForDeletion(tempFile, session);
        }
        return tempFile.getName();

    }

 

接下来就可以使用

String filename = ServletUtilities.saveChartAsJPEG(chart, 800, 600, info, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

来生成图形了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jfreechart 图片模糊可能是由于以下原因引起的: 1. 图像缩放:如果您在绘制图表时进行了缩放,则可能会导致图像变得模糊。 2. 字体渲染:如果您使用了自定义字体并且在渲染过程中发生了问题,则图像可能会变得模糊。 3. 图片尺寸:如果您创建的图像尺寸太小,则可能会导致图像变得模糊。 为了解决这个问题,您可以尝试以下方法: 1. 禁用图像缩放。 2. 如果您使用自定义字体,请确保字体被正确加载并且没有问题。 3. 尝试增加图像的尺寸以获得更好的清晰度。 4. 尝试使用抗锯齿技术来改善图像的清晰度。 您可以尝试在创建图表时使用以下代码来启用抗锯齿: ``` ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); ``` 或者在创建图表时指定渲染器以使用抗锯齿: ``` LineAndShapeRenderer renderer = new LineAndShapeRenderer(); renderer.setSeriesStroke(0, new BasicStroke(2.0f)); renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); renderer.setSeriesStroke(1, new BasicStroke(2.0f)); renderer.setSeriesShape(1, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); renderer.setSeriesStroke(2, new BasicStroke(2.0f)); renderer.setSeriesShape(2, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); renderer.setSeriesStroke(3, new BasicStroke(2.0f)); renderer.setSeriesShape(3, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); JFreeChart chart = ChartFactory.createXYLineChart( "Line Chart Demo", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false ); chart.setBackgroundPaint(Color.white); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setRenderer(renderer); ``` 希望这些方法能够帮助您解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值