I'm trying to generate a Jasper Report using DynamicJasper which is just a chart, with no table appearing above it. There doesn't seem to be a method for hiding a column or for suppressing the table. What are my options for generating just a chart? I would be open to a solution that does not use DynamicJasper, just the JasperReports java API.
public void go() throws ColumnBuilderException, ClassNotFoundException, JRException {
super();
final AbstractColumn areas = ColumnBuilder.getNew()
.setColumnProperty("area", String.class.getName())
.setTitle("Area")
.setWidth(30)
.build();
final AbstractColumn exercises = ColumnBuilder.getNew()
.setColumnProperty("exercises", Integer.class.getName())
.setTitle("Exercises")
.setWidth(30)
.build();
final DynamicReportBuilder reportBuilder = new DynamicReportBuilder();
reportBuilder.addColumn(areas);
reportBuilder.addColumn(exercises);
reportBuilder.setUseFullPageWidth(true);
final GroupBuilder groupBuilder = new GroupBuilder();
groupBuilder.setCriteriaColumn((PropertyColumn) areas);
groupBuilder.setGroupLayout(GroupLayout.EMPTY);
final DJGroup group = groupBuilder.build();
reportBuilder.addGroup(group);
reportBuilder.addChart(this.chart((PropertyColumn) areas, exercises));
final DynamicReport dynamicReport = reportBuilder.build();
final JasperPrint jasperPrint =
DynamicJasperHelper.generateJasperPrint(
dynamicReport,
new ClassicLayoutManager(),
new JRBeanCollectionDataSource(this.getData()));
/* PDF */
final JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("/tmp/my.pdf"));
exporter.exportReport();
}
If DynamicJasper
is no must for you, you could try it with the DynamicReports library.
I only did some few tests with both, but I found the DynamicReports
much more easier to use.
Example code from my tests back then, a report with chart only (or uncomment the .columns
section to get the columns back):
// First simple test
public static void createDynamicReportTest1()
{
StyleBuilder boldStyle = stl.style().bold();
StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle)
.setBorder(stl.pen1Point())
.setBackgroundColor(Color.LIGHT_GRAY);
FontBuilder boldFont = stl.fontArialBold().setFontSize(12);
// title, field name data type
TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType());
TextColumnBuilder<Integer> quantityColumn = col.column("Quantity", "quantity", type.integerType());
TextColumnBuilder<BigDecimal> unitPriceColumn = col.column("Unit price", "unitprice", type.bigDecimalType());
//price = unitPrice * quantity
TextColumnBuilder<BigDecimal> priceColumn = unitPriceColumn.multiply(quantityColumn).setTitle("Price");
PercentageColumnBuilder pricePercColumn = col.percentageColumn("Price %", priceColumn);
TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.")
//sets the fixed width of a column, width = 2 * character width
.setFixedColumns(2)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
AbstractBaseChartBuilder chart = cht.barChart()
.setTitle("Bar chart")
//.setTitleFont(boldFont)
.setCategory(itemColumn)
.series(
cht.serie(quantityColumn))
.setCategoryAxisFormat(
cht.axisFormat().setLabel("Item"));
try {
report()//create new report design
.setColumnTitleStyle(columnTitleStyle)
.highlightDetailEvenRows()
// .columns(//add columns
// rowNumberColumn, itemColumn, quantityColumn, unitPriceColumn, priceColumn, pricePercColumn)
.title(cmp.text("Getting started").setStyle(boldCenteredStyle))//shows report title
.summary(chart)
.pageFooter(cmp.pageXofY().setStyle(boldCenteredStyle))//shows number of page at page footer
.setDataSource(createDRDataSource())//set datasource
.show();//create and show report
} catch (Exception e) {
e.printStackTrace();
}
}
public static JRDataSource createDRDataSource() {
DRDataSource dataSource = new DRDataSource("item", "quantity", "unitprice");
dataSource.add("Notebook", 1, new BigDecimal(500));
dataSource.add("DVD", 5, new BigDecimal(30));
dataSource.add("DVD", 1, new BigDecimal(28));
dataSource.add("DVD", 5, new BigDecimal(32));
dataSource.add("Book", 3, new BigDecimal(11));
dataSource.add("Book", 1, new BigDecimal(15));
dataSource.add("Book", 5, new BigDecimal(10));
dataSource.add("Book", 8, new BigDecimal(9));
return dataSource;
}
For more examples have a look here:
dynamicreports.org examples
Instead of addColumn
use addField
. Thanks to Petter Friberg for this comment
I know this is an old question, but I wanted to share my solution after I spent hours trying to figure out a solution to this.
Unfortunately, the above 2 answers did not work for me while using DynamicJasper 5.1.0. DynamicReports seems to be no longer maintained and the link is broken, and simply using addField instead of addColumn resulted in the unhelpful "duplicate declaration of parameter: dataset_null_chart" error - which seems to be related to having duplicate charts despite the fact that I was only using a single chart. (That being said, I'd love to hear if/how someone overcame these issues using DynamicJasper 5.1.x)
So I came up with the following solution - create a LayoutManager
that does not render the table:
public class NoTableLayoutManager extends ClassicLayoutManager {
@Override
protected List<AbstractColumn> getVisibleColumns() {
return new ArrayList<>(); // hide all columns
}
}
Then use this when you generate your JasperPrint
object:
JasperPrint print = DynamicJasperHelper.generateJasperPrint(report, new NoTableLayoutManager(), data);
This worked perfectly for my simple scenario of creating a stacked bar chart without a table. I haven't tried more complex reports while hiding the table, so I don't know if this will work in every situation, but it should work in the scenario you asked about, at least in DynamicJasper 5.1.0.