当我通过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);
}
}
}
}
这更可能是PDF问题,而不是JFreeChart问题。图表是使用Graphics2D
对象在画布上渲染的,如果底层图形引擎(在您的案例中为JasperReports)没有正确处理alpha通道或画布在渲染为PDF之前被光栅化,则不会有任何透明度。
发生这种情况可能是因为JasperReports没有像JFreeChart所期望的那样处理alpha通道。