只是想知道是否有可能创建这样一个Jasper报告图表,它将是许多图表类型的组合?
在我的情况下,我想在两个不同的轴上组合条形图和 XY线。我已准备好以下类型的条形图:
理想情况下,我也可以在同一个图表中添加一条XY线,这将显示每个月所有小时类型总和的累计值。请参见最后的图片。我觉得这是不可能的,也许我应该为XY线创建一个单独的图表?
答案 0 :(得分:1)
这是对此问题How to populate chart data with JavaBeans collection dataSet?的跟进(如果您喜欢简单的条形图并动态使用系列表达式,请参阅我的answer了解更多详细信息)
要实现多轴图表,您应该使用<multiAxisChart>
,它会变得有点复杂。我们不能再使用动态seriesExpression
并且需要手动定义每个系列,因此我将使用原始bean但仍然在分离的数据源中。
Java bean
public class WorkingHours {
private int month = 0;
private double hoursNormal = 0;
private double hoursTravel = 0;
private double hoursOvertime = 0;
private double hoursTotalCumulative = 0;
public WorkingHours(int month, double hoursNormal, double hoursTravel, double hoursOvertime, double hoursTotalCumulative) {
super();
this.month = month;
this.hoursNormal = hoursNormal;
this.hoursTravel = hoursTravel;
this.hoursOvertime = hoursOvertime;
this.hoursTotalCumulative = hoursTotalCumulative;
}
//getter and setter
}
填写数据(使用您的逻辑)并在参数中传递java.util.List
List<WorkingHours> list = new ArrayList<WorkingHours>();
list.add(new WorkingHours(1, 2.3, 1.2, 2.1,4.1));
list.add(new WorkingHours(2, 5.3, 2.2,3, 9.1));
list.add(new WorkingHours(3, 3.1, 0.5, 2.0, 20.5));
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("CHART_DATA", list);
报告(jrxml)
<?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="working_hours" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1a12c021-57e2-4482-a273-56cbd3f78a17">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="chartDataSet" uuid="119b7f0e-01ef-4e2b-b628-d76f51e83768">
<field name="month" class="java.lang.Integer"/>
<field name="hoursNormal" class="java.lang.Double"/>
<field name="hoursTravel" class="java.lang.Double"/>
<field name="hoursOvertime" class="java.lang.Double"/>
<field name="hoursTotalCumulative" class="java.lang.Double"/>
</subDataset>
<parameter name="CHART_DATA" class="java.util.List" isForPrompting="false"/>
<summary>
<band height="282" splitType="Stretch">
<multiAxisChart>
<chart evaluationTime="Report">
<reportElement x="62" y="17" width="419" height="235" uuid="8a16251e-8c1a-4384-8487-9be8f6c274e5"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend position="Right"/>
</chart>
<multiAxisPlot>
<plot/>
<axis position="rightOrBottom">
<lineChart>
<chart evaluationTime="Report">
<reportElement positionType="Float" x="0" y="25" width="270" height="175" backcolor="#FFFFFF" uuid="4a755d76-1350-4921-a0be-20ae9e485e12"/>
<chartTitle color="#000000"/>
<chartSubtitle color="#000000"/>
<chartLegend textColor="#000000" backgroundColor="#FFFFFF" position="Right"/>
</chart>
<categoryDataset>
<dataset>
<datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})]]></dataSourceExpression>
</datasetRun>
</dataset>
<categorySeries>
<seriesExpression><![CDATA["Total Cum"]]></seriesExpression>
<categoryExpression><![CDATA[$F{month}]]></categoryExpression>
<valueExpression><![CDATA[$F{hoursTotalCumulative}]]></valueExpression>
</categorySeries>
</categoryDataset>
<linePlot isShowLines="true" isShowShapes="true">
<plot>
<seriesColor seriesOrder="0" color="#9900CC"/>
</plot>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisLabelExpression><![CDATA["Total Cum"]]></valueAxisLabelExpression>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" tickLabelMask="#,##0" axisLineColor="#000000"/>
</valueAxisFormat>
</linePlot>
</lineChart>
</axis>
<axis>
<barChart>
<chart evaluationTime="Report">
<reportElement x="0" y="0" width="0" height="0" backcolor="#FFFFFF" uuid="723abd06-b593-422a-b679-043084525a8c"/>
<chartTitle color="#000000"/>
<chartSubtitle color="#000000"/>
<chartLegend textColor="#000000" backgroundColor="#FFFFFF" position="Right"/>
</chart>
<categoryDataset>
<dataset>
<datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})]]></dataSourceExpression>
</datasetRun>
</dataset>
<categorySeries>
<seriesExpression><![CDATA["Normal"]]></seriesExpression>
<categoryExpression><![CDATA[$F{month}]]></categoryExpression>
<valueExpression><![CDATA[$F{hoursNormal}]]></valueExpression>
</categorySeries>
<categorySeries>
<seriesExpression><![CDATA["Travel"]]></seriesExpression>
<categoryExpression><![CDATA[$F{month}]]></categoryExpression>
<valueExpression><![CDATA[$F{hoursTravel}]]></valueExpression>
</categorySeries>
<categorySeries>
<seriesExpression><![CDATA["Overtime"]]></seriesExpression>
<categoryExpression><![CDATA[$F{month}]]></categoryExpression>
<valueExpression><![CDATA[$F{hoursOvertime}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisLabelExpression><![CDATA["h"]]></valueAxisLabelExpression>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" tickLabelMask="#,##0" axisLineColor="#000000"/>
</valueAxisFormat>
</barPlot>
</barChart>
</axis>
</multiAxisPlot>
</multiAxisChart>
</band>
</summary>
</jasperReport>
关键点:
对于另一个<multiAxisChart>
的{{1}},<axis>
使用<lineChart>
<barChart>
,<barChart>
我们分别定义<categorySeries>
fs
<强>输出强>
有关图表上的更多演示,请参阅:Jasper Reports Chart Samples