碧玉报告中区域图中区域的透明度问题

时间:2014-04-29 04:10:55

标签: java jasper-reports jfreechart

当我通过jasper做区域图时,我遇到了问题。

问题在于,当区域图部分中存在多个区域时,下一个区域会覆盖一个区域。因此前一个区域不可见。

这似乎是使用颜色透明度的问题。

下面的

是一段代码

公共类StatsChartCustomiser扩展JRAbstractChartCustomizer实现JRChartCustomizer {

/** The class logger. */
private static final Log log = LogFactory.getLog(StatsChartCustomiser.class);

public void customize(final JFreeChart jFreeChart, final JRChart jasperChart) {
    Plot plot = jFreeChart.getPlot();
    jFreeChart.getCategoryPlot().getRenderer().getPlot().setForegroundAlpha(0.5f);
    jasperChart.getPlot().setForegroundAlpha(0.5f);
    if (plot instanceof CategoryPlot) {
        CategoryPlot categoryPlot = (CategoryPlot) plot;
        CategoryItemRenderer renderer = categoryPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        renderer.setSeriesPaint(1, Color.red);
        renderer.setSeriesPaint(2, Color.green);
        categoryPlot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        categoryPlot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        categoryPlot.setDomainGridlinesVisible(true);
        categoryPlot.setForegroundAlpha(0.5f);
        categoryPlot.getForegroundAlpha();
        CategoryAxis xAxis = categoryPlot.getDomainAxis();
        Font tickLabelFont = xAxis.getTickLabelFont();
        ChartFrame frame1=new ChartFrame("Area Chart",jFreeChart);
        frame1.setVisible(true);
        //frame1.setOpacity((float) .5);
        // category labels in category charts with lots of tick marks are
        // unreadable, so reduce font size (think a JFreeChart bug)
        xAxis.setTickLabelFont(tickLabelFont.deriveFont(8f));
        ValueAxis yAxis = categoryPlot.getRangeAxis();
        xAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0)); // vertical
        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        jasperChart.getParentProperties();
        JRChartPlot jasperplot = jasperChart.getPlot();
        System.out.println("before----    "+jasperplot.getForegroundAlpha());
        jasperplot.setForegroundAlpha(0.5f);
        System.out.println("after----    "+jasperplot.getForegroundAlpha());
        jasperplot.getForegroundAlpha();


    } else if (plot instanceof XYPlot) {
        XYPlot xyPlot = (XYPlot) plot;
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        xyPlot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        xyPlot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        ValueAxis xAxis = xyPlot.getDomainAxis();
        NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        xAxis.setVerticalTickLabels(true);
        if (xAxis instanceof DateAxis) {
            DateAxis dateAxis = (DateAxis) xAxis;
            SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
            String interval = (String) getParameterValue("Interval");
            if ("Day".equals(interval)) {
                sdf.applyPattern("HH:mm");
                // strange, but the range seems to comes out wrong for Day (comes out as 23:00 - 01:00 ???) unless explicitly set
                String fromDateString = (String) getParameterValue("FromDate");
                DateFormat dfdt = new SimpleDateFormat(Bof2Constants.CALENDAR_WIDGET_DATE_TIME_FORMAT);
                // set hours,min,sec to 0 in FromDate to get the start time for the x axis
                try {
                Date fromDate = dfdt.parse(fromDateString);
                Calendar cal = Calendar.getInstance();
                cal.setTime(fromDate);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                Date startOfDay = cal.getTime();
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                Date endOfDay = cal.getTime();  
                dateAxis.setRange(startOfDay, endOfDay);
                SegmentedTimeline timeline = new SegmentedTimeline(Bof2Constants.MILLISECS_IN_AN_HOUR, 24, 0);
                timeline.setStartTime(fromDate.getTime() - 1);
                dateAxis.setTimeline(timeline);
                } catch (ParseException e) {
                    log.error("Failed to parse fromDateString:" + fromDateString + " for StatsChartReport by Day");
                }
            }
            int tickInterval = ((Integer) getParameterValue("TickInterval")).intValue();
            int tickIntervalCount = ((Integer) getParameterValue("TickIntervalCount")).intValue();
            dateAxis.setTickUnit(new DateTickUnit(tickInterval, tickIntervalCount, sdf));
            dateAxis.setAutoTickUnitSelection(false);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

这更像是一个PDF问题而不是JFreeChart问题。使用Graphics2D对象在画布上呈现图表,如果基础图形引擎(在您的情况下为JasperReports)无法正确处理Alpha通道,则在渲染为PDF之前对画布进行栅格化处理,没有任何透明度。

这可能发生 ,因为JasperReports不像JFreeChart期望的那样处理alpha通道。