如何提高jfree图表的质量(分辨率)

时间:2014-02-26 12:30:12

标签: java jfreechart

我使用jfree图表(折线图)为我的应用程序...我的图表在创建时看起来很模糊..我怎样才能获得高清晰度图像。

我使用过:ChartUtilities.writeScaledChartAsPNG(baos, chart, 205, 100, 4, 3);

它不起作用......请建议我这样做。

更新1:

    final JFreeChart chart = ChartFactory.createLineChart(
                    "", // chart title
                    "", // domain axis label
                    "", // range axis label
                    categoryDataset, // data
                    PlotOrientation.VERTICAL, // orientation
                    false, // include legend
                    false, // tooltips
                    false // urls
            );

            chart.setBackgroundPaint(Color.WHITE);
            final CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.WHITE);
            plot.setOutlineVisible(false);
            plot.setRangeGridlinePaint(Color.white);

            final CategoryAxis categoryAxis = plot.getDomainAxis();
            categoryAxis.setAxisLineVisible(false);
            categoryAxis.setTickMarksVisible(false);
            categoryAxis.setMaximumCategoryLabelLines(2);
            categoryAxis.setTickLabelFont(ticklabelfont);

            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setVisible(false);
            rangeAxis.setRange(minRange, maxRange);
            rangeAxis.setTickUnit(new NumberTickUnit(20));

            final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
            categoryAxis.setTickLabelPaint(Color.decode("#222222"));
            if (lineColorFlag) {
                renderer.setSeriesStroke(0, new BasicStroke(1.5f));
            } else {
                chart.setPadding(new RectangleInsets(0, -8, 0, 0));
                renderer.setSeriesStroke(0, new BasicStroke(1.2f));
            }
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setSeriesItemLabelsVisible(1, Boolean.TRUE);
            Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
            renderer.setSeriesShape(0, circle);
            renderer.setSeriesShape(1, null);
            renderer.setBaseItemLabelGenerator(new CustomLabelGenerator());
            renderer.setItemLabelAnchorOffset(-5);
            renderer.setBaseItemLabelsVisible(true);
            plot.getRenderer().setSeriesPaint(0, Color.decode("#FEC240"));
            renderer.setBaseItemLabelFont(annotationlabelfont);
            plot.getRenderer().setSeriesPaint(1, Color.WHITE);

            try {                      
    //            BufferedImage bi = chart.createBufferedImage(205, 100);            
    //            BufferedImage image = chart.createBufferedImage(205, 100);
    //            PngEncoder encoder = new PngEncoder(image, false, 0, 9);
    //            encoder.setDpi(300, 300);
    //            byte[] data = encoder.pngEncode();
    //            InputStream in = new ByteArrayInputStream(data);
    //            BufferedImage bImageFromConvert = ImageIO.read(in);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();            
ChartUtilities.writeScaledChartAsPNG(baos, chart, 205, 100, 4, 3);            
                byte[] bi=baos.toByteArray();
                InputStream in = new ByteArrayInputStream(bi);
                BufferedImage bImageFromConvert = ImageIO.read(in);
                PngEncoder pngEncoder=new PngEncoder(bImageFromConvert);

    //            ChartUtilities.writeScaledChartAsPNG(baos, chart, 205, 100, 2, 2);
                ImageIO.write(bImageFromConvert, "png", baos);
                byte[] byteArray = baos.toByteArray();

O / p图片:

enter image description here

2 个答案:

答案 0 :(得分:2)

在将图表写入图像之前,请尝试调用plot.setShadowGenerator(null)。否则,绘图的内容将被绘制到图像,将生成模糊阴影,两者将被覆盖,然后生成的图像将按比例放大到PNG输出图像的较大尺寸。没有完整的信息很难说,但我猜这可能是你报道的模糊性的原因。

还可以使用RenderingHint来抑制阴影生成:

chart.getRenderingHints().put(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, Boolean.TRUE);

最后的建议 - 您是否考虑过使用JFreeSVG将图表写入SVG格式?

答案 1 :(得分:0)

尝试一下,它对我有用:

ByteArrayOutputStream bout = new ByteArrayOutputStream();
int scale =3 ; //chart will be rendered at thrice the resolution.
ChartUtilities.writeScaledChartAsPNG(bout, chart, 200, 200, scale, scale);

使用比例尺(int)作为提高分辨率的时间。