问题描述
我已经创建了一个工作的jrxml报告,该报告显示了由Java bean的集合( List
)的数据集填充的表。
I have already created a working jrxml report presenting a table populated by a dataset of a collection (List
) of Java beans.
现在我想使用相同的数据集来创建图表(基本条形图的启动器)。每个bean包含4个值,我想在条形图上显示:月,正常小时,旅行时间和加班时间。我希望每个bean会为每个月生成一组3小节,所以最终图表将包含从底部到上方的12x3酒吧,月的名称将作为3酒吧组下的标签,
Now I would like to use that same dataset to create Chart (basic bar chart for starters). Each bean contains 4 values that I would like to show on bar chart: month, normal hours, travel hours, and overtime hours. I was hoping that each bean would generate a group of 3-bars for each month so in the end the chart would contain 12x3 bars growing from bottom to up and the name of the month would act as a label under the 3-bar groups, each group positioned next to another starting from left to right.
不幸的是,创建这个图表看起来比我想象的要困难得多。至少它似乎是完全不同的相比,创建表。我不知道Jasper Studio的图表向导是否工作。至少它不允许我在图表数据系列对话框中添加任何系列:如果我按下添加绝对没有发生任何事情 - 没有对话框打开,没有错误消息,没有什么,没有什么提示我什么是错误。
Unfortunately creating this chart seems much harder than I thought. At least it seems to be totally different compared to creating the table. I'm not sure if the Jasper Studio's Chart wizard is even working. At least it doesn't allow me to add any series in the chart data series dialog: if I press Add absolutely nothing happens - no dialog opens, no error message, nothing, nothing to hint me what is wrong.
主要问题是我没有看到将数据集数据连接到图表的方法。
Main problem is that I don't see a way to connect the dataset data to the Chart.
在尝试将图表嵌入我的主报表后,我尝试将它也添加到一个新的子报表中,仅用于作为图表容器。我将主报表数据集作为数据源传递到子报表,并尝试将其用作子报表图表中的主数据集。仍然没有运气与数据集/图表连接,仍然没有任何事情,如果我按下添加按钮。
After trying embedding chart to my main report, I tried to add it also to a new subreport created only for to act as chart container. I passed the main report dataset as a datasource to subreport and tried to use it as main data set in subreport's chart. Still no luck with the dataset/chart connection, f.e. Still nothing happens if I press the Add-button.
下面你可以看到我使用的简单bean。第一个,WorkingHoursReport是我传递的报告为JRBeanCollectionDataSource的bean。我相信这个bean最有趣的领域是WorkingHours-beans的列表。该列表上总有12个项目:每个月一个。这是我目前传递给我使用datasource表达式的表元素的列表:new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {workingHours})。
Below you can see the simple beans I'm using. First one, the WorkingHoursReport is the bean I'm passing to report as JRBeanCollectionDataSource. I believe the most interesting field of that bean is the list of WorkingHours-beans. There will be always 12 items on that list: one for each month. That is the list I'm currently passing to my table element using datasource expression: new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{workingHours}).
WorkingHoursReport.java :
public class WorkingHoursReport extends CommonReport {
private int year;
private List<WorkingHours> workingHours;
}
WorkingHours.java :
public class WorkingHours {
private int month = 0;
private double hoursNormal = 0;
private double hoursTravel = 0;
private double hoursOvertime = 0;
private double hoursTotal = 0;
private double hoursTotalCumulative = 0;
}
在尝试创建我的第一个图表时,数据到图表使用非常相同的命令来定义数据源,因为我已经成功地使用我的表:
While trying to create my first chart ever, I was naturally trying to populate the data to chart using the very same command for defining the datasource as I was already using successfully with my table:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{workingHours}).
Unfortunately it seemed that at least the jasper studio chart creation wizard did not get any connection to the data (no dialog was opening where, according to the docs, I should have been able to select data fields to chart ).
推荐答案
是我如何解决你的问题,考虑到你使用java bean,需要总结数据,然后有两个系列(小时)和类别(月)。不要将其连接到表数据源,而是为其创建特定的数据源。
This is how I would solve your problem considering that you are using java beans, need to sum up data and then have both series (hours) and categories (months). Don't connect it to your table datasource but create a specific datasource for it.
创建特定的图表bean
$ b
Create a specific chart bean
public class ChartData {
private String serie;
private String category;
private double value;
public ChartData(String serie, String category, double value) {
super();
this.serie = serie;
this.category = category;
this.value = value;
}
.... getter and setters
}
使用数据填充图表bean
Fill the chart bean's with data
循环您的数据集(List)并填充ChartData列表,您可能需要一个地图来查找同一个月并添加到小时。我不会显示这个,但静态地创建它们来显示一个例子
Loop your dataset (List) and fill the ChartData list, you will probably need a map to find same month and add to the hours. I will not show you this but create them statically to show an example
List<ChartData> cList = new ArrayList<ChartData>();
cList.add(new ChartData("hoursNormal","month1", 12.3)); //The use of resources or static text is beyond this example
cList.add(new ChartData("hoursTravel","month1", 3.2));
cList.add(new ChartData("hoursOvertime","month1", 1.3));
cList.add(new ChartData("hoursNormal","month2", 16.4));
cList.add(new ChartData("hoursTravel","month2", 5.2));
cList.add(new ChartData("hoursOvertime","month2", 4.1));
通过参数映射将列表作为数据源传递
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("CHART_DATASET", new JRBeanCollectionDataSource(cList));
显示图表
现在我们可以使用来显示
传递参数 title
或 summary
subDataset $ P {CHART_DATASET}
Now we can display the chart in the title
or summary
band using a subDataset
passed on the parameter $P{CHART_DATASET}
<?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">
<subDataset name="chartDataSet" uuid="119b7f0e-01ef-4e2b-b628-d76f51e83768">
<field name="serie" class="java.lang.String"/>
<field name="category" class="java.lang.String"/>
<field name="value" class="java.lang.Double"/>
</subDataset>
<parameter name="CHART_DATASET" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<summary>
<band height="142" splitType="Stretch">
<barChart>
<chart>
<reportElement x="80" y="0" width="337" height="142" uuid="c8f4dc5d-47e7-489b-b27e-09976d90994a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset>
<datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
<dataSourceExpression><![CDATA[$P{CHART_DATASET}]]></dataSourceExpression>
</datasetRun>
</dataset>
<categorySeries>
<seriesExpression><![CDATA[$F{serie}]]></seriesExpression>
<categoryExpression><![CDATA[$F{category}]]></categoryExpression>
<valueExpression><![CDATA[$F{value}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</barPlot>
</barChart>
</band>
</summary>
</jasperReport>
JasperSoft Studio中的设置
享受结果
这篇关于如何使用JavaBeans集合dataSet填充图表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!