Reputation: 51
I need help about to change the color of the percent bar in gantt chart . I am using dynamic report api to generate report in pdf format.
I need to change the color of the progress bar, it always shows as green and I want to change it.
private JasperReportBuilder build(){
JasperReportBuilder report = DynamicReports.report();
TextColumnBuilder<String> uName = col.column("Name", "name", type.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT);
TextColumnBuilder<Date> uStart = col.column("Start", "start", type.dateType());
TextColumnBuilder<Date> uEnd = col.column("End", "end", type.dateType());
TextColumnBuilder<Double> uProgress = col.column("Progress", "progress", type.doubleType());
GanttChartBuilder chart2 = cht.ganttChart().customizers(new ChartCustomizer())
.setTask(uName)
.series(
cht.ganttSerie()
.setStartDate(uStart)
.setEndDate(uEnd)
.setPercent(uProgress)
).seriesColors(new Color(163,209,255)).setDataSource(createDataSourceForGanntChart(initiativeList,initiativeGroup,periodId,subPeriodId,fullPath,model))
.setTimeAxisFormat(
cht.axisFormat().setLabel(objectInitiativeChart.getCategoryLabel()))
.setTaskAxisFormat(
cht.axisFormat().setLabel(objectInitiativeChart.getSeriesLabel()));
report.summary(chart2);
return report;
}
private class ChartCustomizer implements DRIChartCustomizer, Serializable {
private static final long serialVersionUID = 1L;
@Override
public void customize(JFreeChart chart, ReportParameters reportParameters) {
BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
renderer.setMaximumBarWidth(0.1);
org.jfree.chart.axis.CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
domainAxis.setCategoryMargin(-0.5d);
}
}
Upvotes: 1
Views: 703
Reputation: 21710
You need to get the GanttRenderer
to setCompletePaint(Color c)
, hence now you are casting to BarRenderer
Full example
Add a chart customizer to get the JFreeChart
object. (note you are using set which is deprecated
)
chart2.addCustomizer(new DRIChartCustomizer() {
private static final long serialVersionUID = 1L;
@Override
public void customize(JFreeChart chart, ReportParameters arg) {
//Here we got the JFreeChart object and we can modify it as we like
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//Cast to GanttRenderer
GanttRenderer renderer = (GanttRenderer) plot.getRenderer();
//Set colors as desired.
renderer.setIncompletePaint(Color.CYAN);
renderer.setCompletePaint(Color.MAGENTA);
}
});
Upvotes: 1