如何通过垂直分离将图表系列叠加在一起?

时间:2016-03-16 12:23:11

标签: java charts jasper-reports jfreechart

我想在折线图中使用垂直分离制作一个jasper报告。

我检查了所有报告元素,但没有找到任何匹配的元素。我附上我想要的样本图像

Expected output

1 个答案:

答案 0 :(得分:0)

图表由库和AFIK生成,没有任何设置可以让您自动实现此目的。

解决方案是在每个系列上生成多个图表(一个在另一个之上)并将定制器应用于图表,以便可以根据其位置调整每个图表。

csv数据示例

+----------+--------+--------+
| Category | Serie1 | Serie2 |
+----------+--------+--------+
| A        | 1      | 0.3    |
| B        | 0.5    | 0.2    |
| C        | 0.7    | 0.6    |
+----------+--------+--------+
带有两个图表的

jrxml示例(1代表series1,另一代代表series2)

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="chartTest" pageWidth="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Category" class="java.lang.String"/>
    <field name="Serie1" class="java.lang.Double"/>
    <field name="Serie2" class="java.lang.Double"/>
    <summary>
        <band height="205" splitType="Stretch">
            <lineChart>
                <chart customizerClass="ChartCustomizer">
                    <reportElement x="171" y="0" width="200" height="100" uuid="245c4678-0cad-4342-8e54-6355c23a3c72"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend position="Right"/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA["Serie 1"]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{Category}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{Serie1}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot isShowShapes="false">
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat verticalTickLabels="false"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat tickLabelColor="#FF0033"/>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
            <lineChart>
                <chart>
                    <reportElement x="171" y="93" width="200" height="111" uuid="b53cb5dc-09cd-448d-93ea-0719c239eafe"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend position="Right"/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA["Serie 2"]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{Category}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{Serie2}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot isShowShapes="false">
                    <plot>
                        <seriesColor seriesOrder="0" color="#000000"/>
                    </plot>
                    <categoryAxisFormat>
                        <axisFormat verticalTickLabels="true"/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                    <rangeAxisMinValueExpression><![CDATA[0]]></rangeAxisMinValueExpression>
                    <rangeAxisMaxValueExpression><![CDATA[2]]></rangeAxisMaxValueExpression>
                </linePlot>
            </lineChart>
        </band>
    </summary>
</jasperReport>

java类,包括图表1的ChartCustomizer和运行报告的main方法

public class ChartCustomizer implements JRChartCustomizer {

    @Override
    public void customize(JFreeChart jfchart, JRChart jrchart) {
        CategoryPlot plot = (CategoryPlot) jfchart.getPlot();
        CategoryAxis range = plot.getDomainAxis();
        //Don't show the range axis
        range.setVisible(false);

        //Lets remove the 0, in your case you can do a customizer to remove the 2 on the other chart
        NumberAxis rangeAxis = new NumberAxis() {

            private static final long serialVersionUID = 3744076016723532336L;

            @SuppressWarnings({ "rawtypes", "unchecked" })
            @Override
            public List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) {

                List allTicks = super.refreshTicks(g2, state, dataArea, edge);
                NumberTick t0 = new NumberTick(TickType.MAJOR, 0, "", TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, 0);
                allTicks.set(0, t0);
                return allTicks;
            }
        };

        //Set range and paint, since we replace the rangeAxis
        rangeAxis.setRange(0, 2);
        rangeAxis.setTickLabelPaint(Color.RED); 
        plot.setRangeAxis(rangeAxis);

    }

    public static void main(String[] args) throws Exception {
        JasperReport report = JasperCompileManager.compileReport("ChartTest.jrxml");

        JRCsvDataSource ds = new JRCsvDataSource(new File("ChartData.csv"));
        ds.setNumberFormat(NumberFormat.getInstance(Locale.US)); //. as decimal separator
        ds.setFieldDelimiter(';');
        ds.setUseFirstRowAsHeader(true);

        Map<String, Object> paramMap = new HashMap<String, Object>();
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, paramMap, ds);
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("ChartTest.pdf"));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true);
        configuration.setMetadataAuthor("Petter");
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    }
}

<强>结果

Output

您可以看到它还不完美,对于图例,您可以将其从图表中删除并直接在jasper报告中创建,此外,您可以删除第二个图表上的2.0而不是第一个图表中的0,但进一步的实施我会留给你。