我怎样才能画出这种Jfreechart

时间:2017-01-05 07:51:04

标签: jfreechart dynamic-reports candlestick-chart

我使用的是Dynamicreports4.1.1,其图表基于Jfreechart。

我想绘制如下图所示的图表: enter image description here

有点像烛台图堆积图分层图

我选择 stackchart 进行自定义,如下所示: enter image description here

代码:

public class StackedBarChartReportTest {

public StackedBarChartReportTest() {
    build();
}

private void build() {
    FontBuilder boldFont = stl.fontArialBold().setFontSize(12);

    TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType());
    TextColumnBuilder<BigDecimal> bestColumn = col.column("Best", "best", type.bigDecimalType());
    TextColumnBuilder<BigDecimal> worstColumn = col.column("Worst", "worst", type.bigDecimalType());
    try {
        report()
            .setTemplate(Templates.reportTemplate)
            .columns(itemColumn, bestColumn, worstColumn)
            .title(Templates.createTitleComponent("StackedBarChart"))
            .summary(
                cht.stackedBarChart()
                    .customizers(new ChartCustomizer())
                    .setTitle("Stacked bar chart")
                    .setTitleFont(boldFont)
                    .setCategory(itemColumn)
                    .setShowValues(true)
                    .series(
                        cht.serie(bestColumn), cht.serie(worstColumn))
                    .setCategoryAxisFormat(
                        cht.axisFormat().setLabel("Item")))
            .pageFooter(Templates.footerComponent)
            .setDataSource(createDataSource())
            .show();
    } catch (DRException e) {
        e.printStackTrace();
    }
}

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();
        // here customize,but have no idea...

    }
}
private JRDataSource createDataSource() {
    DRDataSource dataSource = new DRDataSource("item", "best", "worst");
    dataSource.add("1Y", new BigDecimal(-10), new BigDecimal(-14.5));
    dataSource.add("2Y", new BigDecimal(10), new BigDecimal(4));
    dataSource.add("3Y", new BigDecimal(12), new BigDecimal(-2));
    return dataSource;
}

public static void main(String[] args) {
    new StackedBarChartReportTest();
}

不知道如何开始或是否有这样的图表?

0 个答案:

没有答案