简体   繁体   中英

DynamicReport - How to plot too many data points

I am trying to create LineChartReport using more than 1000 data points. Problem is that the X Axis should show the time stamp, and since there are too many data points, the data gets overlapped and no comprehensible data is shown. So I need help on following 2 points: 1. Limit the data points on X-Axis (only) to say 25. The number of data points for the graph/chart still remains at 1000 2. Rotate the Timestamp data by 90 degrees so that the Timestamp data is recorded correctly and not truncated.

Have tried to get the domain axis and manipulate it, like this, but the library does not allow that:

CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setMinorTickMarksVisible(false);
domainAxis.clearCategoryLabelToolTips();
chart.getCategoryPlot().getDataset().getColumnKeys()
CategoryDataset ds = chart.getCategoryPlot().getDataset();
List ls = ds.getColumnKeys();
List ls2 = new ArrayList();

int i = 0;
for (Iterator it = ls.iterator(); it.hasNext(); ) {
    it.next(); 
    if (i % 2 != 0) {
        ls2.add(ls.get(i));
    }
    i++;
}
chart.getCategoryPlot().setDataset(ds);



Sample image with 10 data points appear here: https://drive.google.com/drive/u/0/folders/0B-m6SCJULOTRdHZ6cUwxX041SHM

Any suggestions ??

The below codes are based on DynamicReport 4.0.2. I didn't test them in other versions.

Regarding your first question, you want 1000 points of data, and just want the few data in line chart. In this case, you need to use the different data source for you data table and line chart.

Firstly, create the subreport for the data table and set up.

SubreportBuilder subreport = cmp.subreport(
    report().setTemplate(Templates.reportTemplate)
            .addColumn(
                col.column("Name", "name", type.stringType()), 
                col.column("Counts", "value", type.integerType())
            )
);
JasperReportBuilder reportContent = report();
subreport.setDataSource(allDatasource);
reportContent.summary(subreport, cmp.verticalGap(20));

Secondly, prepare another data source for line chart and set up.

reportContent.setTemplate(Templates.reportTemplate)
    /* add title */
    .title(title, subtitle,
    /* add chart in the head of title */
    cmp.verticalList(LINE_CHART)
    /* set style */ 
    .setStyle(stl.style().setBottomPadding(30).setTopPadding(30)))
    /* set data source for line chart*/
    .setDataSource(dataSource);

About your second question, you need to create customizer at first.

public class DynamicLineCustomizer implements DRIChartCustomizer, Serializable {
private static final long serialVersionUID = -8493880774698206000L;

@Override
public void customize(JFreeChart jFreeChart, ReportParameters reportParameters) {
    CategoryPlot plot = jFreeChart.getCategoryPlot();
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions
            .createUpRotationLabelPositions(Math.PI / 6.0));
    }
}

Then use this customizer in line chard builder.

LineChartBuilder lineChart = cht.lineChart()
    .customizers(new DynamicLineCustomizer())
    .setCategory(columns[0])
    .series(createSeries(columns))
    .setCategoryAxisFormat(cht.axisFormat().setLabel("TimeStamp"))
    .seriesColors(seriesColors);

The line chart and data table will be like below:

折线图和数据表

This finally worked for me (hope it helps somebody) :

Ref: http://www.dynamicreports.org/forum/viewtopic.php?f=1&t=1046

private void build(String startDate, String endDate) {
    TextColumnBuilder<Integer> i = col.column("I", "I", type.integerType());
    TextColumnBuilder<Integer> b = col.column("B", "B", type.integerType());
    TextColumnBuilder<Integer> t = col.column("T", "T", type.integerType());
    TextColumnBuilder<Date> timeColumn = col.column("TimeStamp", "TimeStamp", type.dateType());

    createDataSource(startDate, endDate);

    try {   
        TimeSeriesChartBuilder timeSeriesChartBuilder1 = cht.timeSeriesChart();
        timeSeriesChartBuilder1.series(cht.serie(b), cht.serie(t), cht.serie(i));
        timeSeriesChartBuilder1.setShowShapes(false);
        timeSeriesChartBuilder1.setDataSource(dataSource);
        timeSeriesChartBuilder1.setTimePeriod(timeColumn);
        timeSeriesChartBuilder1.setTimePeriodType(TimePeriod.SECOND);
        timeSeriesChartBuilder1.setTitle("ABC Information");

        JasperReportBuilder builder = report()
                .summary(cht.multiAxisChart(timeSeriesChartBuilder1))
                .setTemplate(Templates.reportTemplate)
                .title(Templates.createTitleComponent("ABC Complete Info"))
                ;    
        builder.show();   
    } catch (Exception e) {
        e.printStackTrace();
    }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM